Example #1
0
        private void DefaultImplementProperty(IClassBuilder cls, PropertyInfo property)
        {
            var storageField = cls.HasField($"storage{property.Name}_{Guid.NewGuid():N}", property.PropertyType);

            cls.WithConstructor()
            .Body.Assign(
                storageField,
                assignment => assignment.Write("default(").Write(property.PropertyType).Write(")"));

            var prop = cls.HasProperty(property.Name, property.PropertyType).WithModifier("public");

            prop.HasGetter().Write("return ").Write(storageField).EndStatement();
            prop.HasSetter().Assign(storageField, a => a.Write("value"));
        }
Example #2
0
        private Dictionary <string, INamedReference> ProjectFactories(IEnumerable <InstanceRecord> records, IClassBuilder locator, out IConstructorBuilder ctor)
        {
            var result = new Dictionary <string, INamedReference>();

            ctor = locator.WithConstructor().WithModifier("public");

            var parentLocatorParam = ctor.WithParam <IServiceLocator>("parentLocator");

            ctor.CallsBase().WithParam(parentLocatorParam);

            var factoriesParam = ctor.WithParam("factories", typeof(Dictionary <string, Func <IServiceLocator, object> >));

            var constructor = ctor;

            Action <NamedFactory> addFactory = factory =>
            {
                var field =
                    locator.HasField <Func <IServiceLocator, object> >()
                    .WithModifier("readonly")
                    .WithModifier("private");

                constructor.Body.Assign(
                    field,
                    a => a.Write(factoriesParam).Write("[\"").Write(factory.Name).Write("\"]"));

                result.Add(factory.Name, field);
            };

            foreach (var record in records)
            {
                if (record.Factory != null)
                {
                    addFactory(record.Factory);
                }

                if (record.ConstructorParameters != null)
                {
                    foreach (var ctorFactory in record.ConstructorParameters.Where(p => p.ValueProvider != null).Select(p => p.ValueProvider))
                    {
                        addFactory(ctorFactory);
                    }
                }
            }

            return(result);
        }