Example #1
0
 public PropertyGenerator(TypeGenerator generator, System.Reflection.Emit.PropertyBuilder builder)
 {
     _builder      = builder;
     TypeGenerator = generator;
     Name          = builder.Name;
     MemberType    = System.Reflection.MemberTypes.Property;
 }
        private void CreateProperty(Generators.TypeGenerator generator, System.Type returnType, ParameterInfo[] parameters)
        {
            var parameterTypes = parameters.Map(p => p.Type);

            Generators.PropertyGenerator.PropertyHolder accessor = null;
            System.Type type    = null;
            var         name    = string.Concat(char.ToUpper(Name.First()), Name.Substring(1));
            var         builder = generator.Builder;

            System.Reflection.MethodAttributes attributes = GetAttributes();
            if (IsGetter)
            {
                type = returnType;
                string hiddenName = string.Concat("get_", name);
                if ((attributes & System.Reflection.MethodAttributes.Virtual) == System.Reflection.MethodAttributes.Virtual)
                {
                    generator.CheckImplementMethod(Name, parameterTypes, ref hiddenName, ref returnType, ref attributes);
                }
                System.Reflection.Emit.MethodBuilder getBul = builder.DefineMethod(hiddenName, attributes, returnType, parameterTypes);
                accessor = new Generators.PropertyGenerator.PropertyHolder(Generators.PropertyType.Get,
                                                                           new Generators.MethodGenerator(getBul, parameters, generator)
                {
                    SyntaxBody = Body
                });
            }
            if (IsSetter)
            {
                type     = parameterTypes.FirstOrDefault();
                accessor = new Generators.PropertyGenerator.PropertyHolder(Generators.PropertyType.Set,
                                                                           new Generators.MethodGenerator(builder.DefineMethod(string.Concat("set_", name), attributes, returnType, parameterTypes), parameters, generator)
                {
                    SyntaxBody = Body
                });
            }
            if (generator.TryGetProperty(Name, out Generators.PropertyGenerator property) == false)
            {
                var pb = generator.Builder.DefineProperty(name, System.Reflection.PropertyAttributes.None, type, null);
                property = new Generators.PropertyGenerator(generator, pb);
                property.SetCustomAttribute(typeof(Runtime.RegisterAttribute), Utils.ReflectionHelpers.Register_Attr_Ctor, new[] { Name });
                generator.Add(property);
            }

            System.Reflection.Emit.PropertyBuilder propertyBuilder = property.GetBuilder();
            if (IsGetter)
            {
                propertyBuilder.SetGetMethod(accessor.Method.GetBuilder());
            }
            else if (IsSetter)
            {
                propertyBuilder.SetSetMethod(accessor.Method.GetBuilder());
            }
            else
            {
                throw new System.Exception("Accessor not found");
            }
            property.Accessors.Add(accessor);
        }