Exemple #1
0
        public MutablePropertyInfo CreateProperty(
            MutableType declaringType, string name, PropertyAttributes attributes, MutableMethodInfo getMethod, MutableMethodInfo setMethod)
        {
            ArgumentUtility.CheckNotNull("declaringType", declaringType);
            ArgumentUtility.CheckNotNullOrEmpty("name", name);
            // Get method may be null.
            // Set method may be null.

            MemberAttributesUtility.ValidateAttributes("properties", MemberAttributesUtility.InvalidPropertyAttributes, attributes, "attributes");

            if (getMethod == null && setMethod == null)
            {
                throw new ArgumentException("Property must have at least one accessor.", "getMethod");
            }

            var readWriteProperty = getMethod != null && setMethod != null;

            if (readWriteProperty && getMethod.IsStatic != setMethod.IsStatic)
            {
                throw new ArgumentException("Accessor methods must be both either static or non-static.", "getMethod");
            }

            if (getMethod != null && !ReferenceEquals(getMethod.DeclaringType, declaringType))
            {
                throw new ArgumentException("Get method is not declared on the current type.", "getMethod");
            }
            if (setMethod != null && !ReferenceEquals(setMethod.DeclaringType, declaringType))
            {
                throw new ArgumentException("Set method is not declared on the current type.", "setMethod");
            }

            if (getMethod != null && getMethod.ReturnType == typeof(void))
            {
                throw new ArgumentException("Get accessor must be a non-void method.", "getMethod");
            }
            if (setMethod != null && setMethod.ReturnType != typeof(void))
            {
                throw new ArgumentException("Set accessor must have return type void.", "setMethod");
            }

            var getSignature  = getMethod != null ? new PropertySignature(getMethod.ReturnType, getMethod.GetParameters().Select(p => p.ParameterType)) : null;
            var setParameters = setMethod != null?setMethod.GetParameters().Select(p => p.ParameterType).ToList() : null;

            var setSignature = setMethod != null ? new PropertySignature(setParameters.Last(), setParameters.Take(setParameters.Count - 1)) : null;

            if (readWriteProperty && !getSignature.Equals(setSignature))
            {
                throw new ArgumentException("Get and set accessor methods must have a matching signature.", "setMethod");
            }

            var signature = getSignature ?? setSignature;

            if (declaringType.AddedProperties.Any(p => p.Name == name && PropertySignature.Create(p).Equals(signature)))
            {
                throw new InvalidOperationException("Property with equal name and signature already exists.");
            }

            return(new MutablePropertyInfo(declaringType, name, attributes, getMethod, setMethod));
        }
        public void Create()
        {
            var property  = typeof(Dictionary <int, string>).GetProperty("Item");
            var signature = PropertySignature.Create(property);

            Assert.That(signature.PropertyType, Is.SameAs(typeof(string)));
            Assert.That(signature.IndexParameterTypes, Is.EqualTo(new[] { typeof(int) }));
        }
Exemple #3
0
        private PropertyBuilder DefinePropertyImpl(string name, PropertyAttributes attributes, CallingConventions callingConvention, bool patchCallingConvention,
                                                   Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
        {
            if (properties == null)
            {
                properties = new List <PropertyBuilder>();
            }
            PropertySignature sig = PropertySignature.Create(callingConvention, returnType, parameterTypes, customModifiers);
            PropertyBuilder   pb  = new PropertyBuilder(this, name, attributes, sig, patchCallingConvention);

            properties.Add(pb);
            return(pb);
        }
Exemple #4
0
        public void Interface()
        {
            var type = TypeSignature.Interface("MyInterface", ns, Accessibility.APublic);

            var method   = MethodSignature.Instance("MyMethod", type, Accessibility.APublic, TypeSignature.Int32, new MethodParameter(TypeSignature.String, "myParameter"));
            var property = PropertySignature.Create("MyProperty", type, TypeSignature.Boolean, Accessibility.APublic, null);
            var typeDef  = TypeDef.Empty(type)
                           .AddMember(MethodDef.InterfaceDef(method))
                           .AddMember(PropertyDef.InterfaceDef(property));


            cx.AddType(typeDef);
            check.CheckOutput(cx);
        }
Exemple #5
0
        public void StandardProperties()
        {
            // TODO: remove those CompilerGenerated attributes
            var type = TypeSignature.Class("MyType", ns, Accessibility.APublic);
            var prop = PropertySignature.Create("A", type, TypeSignature.String, Accessibility.APublic, Accessibility.AProtected);
            var td   = TypeDef.Empty(type).AddMember(
                new PropertyDef(prop,
                                getter: MethodDef.Create(prop.Getter, thisP => Expression.Constant("abcd")),
                                setter: MethodDef.Create(prop.Setter, (thisP, xP) =>
                                                         Expression.While(FluentExpression.Box(thisP).CallMethod(MethodSignature.Object_Equals, Expression.Default(TypeSignature.Object)), Expression.Nop))
                                )
                );

            cx.AddType(td);
            check.CheckOutput(cx);
        }
Exemple #6
0
        public MutablePropertyInfo CreateProperty(
            MutableType declaringType,
            string name,
            Type type,
            IEnumerable <ParameterDeclaration> indexParameters,
            MethodAttributes accessorAttributes,
            Func <MethodBodyCreationContext, Expression> getBodyProvider,
            Func <MethodBodyCreationContext, Expression> setBodyProvider)
        {
            ArgumentUtility.CheckNotNull("declaringType", declaringType);
            ArgumentUtility.CheckNotNullOrEmpty("name", name);
            ArgumentUtility.CheckNotNull("type", type);
            ArgumentUtility.CheckNotNull("indexParameters", indexParameters);
            // Get body provider may be null.
            // Set body provider may be null.

            MemberAttributesUtility.ValidateAttributes(
                "property accessor methods", MemberAttributesUtility.InvalidMethodAttributes, accessorAttributes, "accessorAttributes");

            if (getBodyProvider == null && setBodyProvider == null)
            {
                throw new ArgumentException("At least one accessor body provider must be specified.", "getBodyProvider");
            }

            var indexParams = indexParameters.ToList();
            var signature   = new PropertySignature(type, indexParams.Select(pd => pd.Type));

            if (declaringType.AddedProperties.Any(p => p.Name == name && PropertySignature.Create(p).Equals(signature)))
            {
                throw new InvalidOperationException("Property with equal name and signature already exists.");
            }

            var attributes = accessorAttributes | MethodAttributes.SpecialName;
            MutableMethodInfo getMethod = null, setMethod = null;

            if (getBodyProvider != null)
            {
                getMethod = CreateAccessor(declaringType, "get_" + name, attributes, type, indexParams, getBodyProvider);
            }
            if (setBodyProvider != null)
            {
                var setterParams = indexParams.Concat(new[] { new ParameterDeclaration(type, "value") });
                setMethod = CreateAccessor(declaringType, "set_" + name, attributes, typeof(void), setterParams, setBodyProvider);
            }

            return(new MutablePropertyInfo(declaringType, name, PropertyAttributes.None, getMethod, setMethod));
        }