Beispiel #1
0
        /// <summary>
        /// Create instances of LocResource, adding them to the private objects collection.
        /// </summary>
        /// <param name="entry"></param>
        private void ProcessResourceFileEntry(ResourceFileEntry entry)
        {
            // Create Property provider by matching property adapters with data sources
            // PropertyProvider is called by ResourceStaticAnalysis when code attempts to retrieve a property that has not yet been created
            var pp = new PropertyProvider();

            // Add a pair: property adapter and an object that serves as source
            foreach (var adapter in resourceFilePropertyAdapters)
            {
                pp.AddPropertyAdapterDataSourcePair(adapter, entry);
            }

            if (configDictPropertyAdapters != null)
            {
                foreach (var adapter in configDictPropertyAdapters)
                {
                    pp.AddPropertyAdapterDataSourcePair(adapter, myConfig);
                }
            }

            // Initialize an instance of LocResource using the PropertyProvider built above
            var lr = new LocResource(pp);

            foreach (var adapter in selfPropertyAdapters)
            {
                // Now, add one more poperty adapter that uses the LocResource object itself as data source.
                // This one builds properties from existing properties.
                pp.AddPropertyAdapterDataSourcePair(adapter, lr);
            }

            // This reduces amount of memory used by object by compacting internal representation.
            pp.Compact();

            // Add to the list of all objects being constructed by this adapter.
            objects.Add(lr);
        }
        /// <summary>
        /// Define the logic of how to provide each of the properties below
        /// </summary>
        /// <param name="propertyId">Numerical id of the property. This replaces string property name. A Classification Object type should expose an enum type listing all properties.</param>
        /// <param name="entry">A single entry in the file</param>
        /// <returns>Returns a Property given the propertyId.</returns>
        static Property GetResourceFileEntryProperty(LocResource.LocResourceProps propertyId, ResourceFileEntry entry)
        {
            if (entry == null)
            {
                throw new NullReferenceException(String.Format("DataSource must not be null. DataSource name: {0}", entry.GetType().Name));
            }
            try
            {
                switch (propertyId)
                {
                case LocResource.LocResourceProps.ResourceId:
                    return(new StringProperty((byte)propertyId, String.IsNullOrEmpty(entry.ResourceId) ? string.Empty : entry.ResourceId));

                case LocResource.LocResourceProps.SourceString:
                    return(new StringProperty((byte)propertyId, String.IsNullOrEmpty(entry.SourceString) ? string.Empty : entry.SourceString));

                case LocResource.LocResourceProps.Comments:
                    return(new StringProperty((byte)propertyId, String.IsNullOrEmpty(entry.Comment) ? string.Empty : entry.Comment));

                case LocResource.LocResourceProps.FilePath:
                    return(new StringProperty((byte)propertyId, String.IsNullOrEmpty(entry.FilePath) ? String.Empty : entry.FilePath));

                default:
                {
                    string message = string.Format("Cannot provide property {0} from datasource of type {1}.",
                                                   propertyId.ToString(), entry.GetType().Name);
                    Trace.TraceInformation(message);
                    throw new InvalidOperationException(message);
                }
                }
            }
            catch (NullReferenceException e)
            {
                Trace.TraceInformation("Exception in ProvideProperty('{1}'): {0}", e.Message, propertyId.ToString());
                return(null);
            }
        }