Ejemplo n.º 1
0
 private void SetPropertyBuilder <TNestedBuilder>(string key, TNestedBuilder builder) where TNestedBuilder : IBuilder
 {
     if (PropertyBuilders.ContainsKey(key))
     {
         PropertyBuilders.Remove(key);
     }
     PropertyBuilders.Add(key, builder);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Gets the value/instance set for the specified key.
 /// </summary>
 /// <typeparam name="T">Type of the value/instance.</typeparam>
 /// <param name="key">Key for the value/instance</param>
 /// <param name="orUse">Func that returns a default value if nothing was registered for the specified key.</param>
 /// <returns></returns>
 protected T GetProperty <T>(string key, Func <T> orUse)
 {
     if (!PropertyBuilders.ContainsKey(key))
     {
         return(orUse());
     }
     return((T)PropertyBuilders[key].Create());
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Checks if property value (or builder that returns a value) has been registered for the specified key.
 /// </summary>
 /// <param name="key">Key to check.</param>
 /// <returns>True if a property value can be resolved, otherwise, false.</returns>
 protected bool HasProperty(string key)
 {
     if (PropertyBuilders.ContainsKey(key))
     {
         return(true);
     }
     return(false);
 }
Ejemplo n.º 4
0
 /// <summary>
 ///     Adds a property.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="type">The type.</param>
 /// <param name="readOnly">if set to <c>true</c> [read only].</param>
 /// <param name="attributes">The attributes.</param>
 /// <returns></returns>
 public ClassBuilder WithProperty(string name,
                                  Type type,
                                  bool readOnly,
                                  params Expression <Func <Attribute> >[] attributes)
 {
     PropertyBuilders.Add(new PropertyBuilder(name, type, attributes, readOnly));
     return(this);
 }
Ejemplo n.º 5
0
        /// <summary>
        ///     Adds multiple properties.
        /// </summary>
        /// <param name="propertyBuilders">The property builders.</param>
        /// <returns></returns>
        public ClassBuilder WithProperty(IEnumerable <PropertyBuilder> propertyBuilders)
        {
            foreach (var propertyBuilder in propertyBuilders)
            {
                PropertyBuilders.Add(propertyBuilder);
            }

            return(this);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets the builder associated with the specified property.
        /// </summary>
        /// <typeparam name="T">Property type</typeparam>
        /// <param name="prop">Lamnda expression pointing to the property.</param>
        /// <param name="orUse">Func that returns a default if no builder was registered for the specified property.</param>
        /// <returns></returns>
        protected TBuilder GetPropertyBuilder <TBuilder>(Expression <Func <TSubject, object> > prop, Func <TBuilder> orUse)
        {
            string key = GetPropertyName(prop);

            if (PropertyBuilders.ContainsKey(key))
            {
                return((TBuilder)PropertyBuilders[key]);
            }

            return(orUse());
        }
Ejemplo n.º 7
0
        public void GenericMethodInGenericTypes()
        {
            var ns       = NamespaceSignature.Parse("MyNamespace");
            var t1       = new GenericParameter(Guid.NewGuid(), "T1");
            var t2       = new GenericParameter(Guid.NewGuid(), "T2");
            var tresult  = new GenericParameter(Guid.NewGuid(), "TResult");
            var rootType = TypeSignature.Class("MyType", ns, Accessibility.APublic, true, false, t1);

            var(f, p) = PropertyBuilders.CreateAutoProperty(rootType, "A", t1, isReadOnly: false);

            var map_fn_type = new FunctionType(new [] { new MethodParameter(t1, "a") }, tresult);
            var map_sgn     = MethodSignature.Instance("Map", rootType, Accessibility.APublic,
                                                       rootType.Specialize(tresult),
                                                       new [] { tresult },
                                                       new MethodParameter(map_fn_type.TryGetDelegate(), "func")
                                                       );
            var tmp     = ParameterExpression.Create(map_sgn.ResultType, "result");
            var map_def = MethodDef.Create(map_sgn, (@this, fn) =>
                                           Expression.Block(
                                               new [] {
                tmp.Ref()
                .CallMethod(p.Setter.Signature.Specialize(new TypeReference[] { tresult }, null),
                            fn.Read().FunctionConvert(map_fn_type).Invoke(@this.Ref().CallMethod(p.Getter.Signature.SpecializeFromDeclaringType()))
                            )
            },
                                               result: tmp
                                               )
                                           .Where(tmp, Expression.NewObject(MethodSignature.ImplicitConstructor(rootType).Specialize(new TypeReference[] { tresult }, null)))
                                           );


            var type = TypeSignature.Class("MyNestedType", rootType, Accessibility.APublic, true, false, t2);

            var t3     = new GenericParameter(Guid.NewGuid(), "T3");
            var method = MethodSignature.Instance(
                "M", type, Accessibility.APublic,
                returnType: type.Specialize(t1, t3),
                typeParameters: new [] { t3 });

            var td = TypeDef.Empty(type)
                     .AddMember(MethodDef.Create(method, @this =>
                                                 Expression.NewObject(
                                                     MethodSignature.ImplicitConstructor(type).Specialize(new TypeReference[] { t1, t3 }, null),
                                                     ImmutableArray <Expression> .Empty
                                                     )
                                                 ))
            ;

            cx.AddType(TypeDef.Empty(rootType).AddMember(td, f, p, map_def));
            check.CheckOutput(cx);
        }
Ejemplo n.º 8
0
        protected CollectionBuilder <TChild, TChildBuilder> GetCollection <TChild, TChildBuilder>(Expression <Func <TSubject, object> > prop)
            where TChild : class
            where TChildBuilder : Builder <TChild>
        {
            CollectionBuilder <TChild, TChildBuilder> col = null;
            var key = GetPropertyName(prop);

            if (PropertyBuilders.ContainsKey(key))
            {
                col = PropertyBuilders[key] as CollectionBuilder <TChild, TChildBuilder>;
            }

            return(col ?? new CollectionBuilder <TChild, TChildBuilder>(this));
        }
Ejemplo n.º 9
0
        /// <summary>
        ///     Gets the result as code instead of an type.
        /// </summary>
        /// <returns></returns>
        public string GetAsCode()
        {
            var propertiesBuilt = PropertyBuilders.Select(i =>
            {
                if (IsInterface)
                {
                    i.AsInterface();
                }

                return(i.Build().ToString());
            }).ToArray();
            var typesInUse = PropertyBuilders.Select(i => i.PropertyType).Concat(PropertyBuilders
                                                                                 .Where(i => i.Attributes != null)
                                                                                 .SelectMany(o => o.Attributes.Select(j => j.Compile().Invoke().GetType())));
            var uniqueUsings = typesInUse.Select(o => o.Namespace).Distinct();

            return(BuildClassOrInterface(uniqueUsings, propertiesBuilt, Name, IsInterface));
        }
Ejemplo n.º 10
0
        public void GenericClass()
        {
            var paramT         = GenericParameter.Create("T");
            var myContainerSgn = TypeSignature.Class(
                "MyContainer",
                NamespaceSignature.Parse("NS"),
                Accessibility.APublic,
                genericParameters: new [] { paramT }
                );

            var(item_field, item_prop) = PropertyBuilders.CreateAutoProperty(
                myContainerSgn,
                name: "Item",
                propertyType: paramT,
                isReadOnly: false
                );

            var listType = TypeSignature.FromType(typeof(List <>))
                           .Specialize(paramT);
            var toListSgn = MethodSignature.Instance(
                "ToList",
                myContainerSgn,
                Accessibility.APublic,
                returnType: listType
                );

            var toListDef = MethodDef.Create(toListSgn, thisParam => {
                var resultVar = ParameterExpression.Create(listType, "result");
                var listCtor  = MethodReference.FromLambda(() => new List <int>())
                                .Signature
                                .Specialize(paramT);
                var listAdd = MethodReference.FromLambda <List <int> >(l => l.Add(0))
                              .Signature
                              .Specialize(paramT);

                return(Expression.LetIn(
                           // result = new List<T>()
                           resultVar, Expression.NewObject(listCtor),
                           new [] {
                    // result.Add(this.Item)
                    resultVar.Read().CallMethod(listAdd,
                                                thisParam.Read().ReadField(item_field.Signature.SpecializeFromDeclaringType())
                                                )
                }.ToBlock(resultVar)
                           ));
            });

            var copyFromSgn = MethodSignature.Instance(
                "CopyFrom",
                myContainerSgn,
                Accessibility.APublic,
                returnType: TypeSignature.Void,
                new MethodParameter(
                    myContainerSgn.SpecializeByItself(),
                    "other"
                    )
                );

            var copyFromDef = MethodDef.Create(copyFromSgn, (thisParam, otherParam) => {
                var field = item_field.Signature.SpecializeFromDeclaringType();
                return(thisParam.Read().AssignField(
                           field,
                           otherParam.Read().ReadField(field)
                           ));
            });


            var myContainerDef =
                TypeDef.Empty(myContainerSgn)
                .AddMember(item_field, item_prop, toListDef, copyFromDef);

            cx.AddType(myContainerDef);

            check.CheckOutput(cx);
        }
Ejemplo n.º 11
0
 /// <summary>
 ///     Adds a property.
 /// </summary>
 /// <param name="propertyBuilder">The property builder.</param>
 /// <returns></returns>
 public ClassBuilder WithProperty(PropertyBuilder propertyBuilder)
 {
     PropertyBuilders.Add(propertyBuilder);
     return(this);
 }
Ejemplo n.º 12
0
 /// <summary>
 ///     Adds a property.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="type">The type.</param>
 /// <param name="readOnly">if set to <c>true</c> [read only].</param>
 /// <param name="attributes">The attributes.</param>
 /// <returns></returns>
 public ClassBuilder WithProperty(string name, Type type, bool readOnly, IEnumerable <Attribute> attributes)
 {
     PropertyBuilders.Add(new PropertyBuilder(name, type, attributes, readOnly));
     return(this);
 }
Ejemplo n.º 13
0
 /// <summary>
 ///     Adds a property.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="type">The type.</param>
 /// <param name="readOnly">if set to <c>true</c> [read only].</param>
 /// <returns></returns>
 public ClassBuilder WithProperty(string name, Type type, bool readOnly)
 {
     PropertyBuilders.Add(new PropertyBuilder(name, type, readOnly: readOnly));
     return(this);
 }