예제 #1
0
        public void SetUp()
        {
            _typeParameter           = MutableGenericParameterObjectMother.Create();
            _parameter               = CustomParameterInfoObjectMother.Create();
            _genericMethodDefinition = CustomMethodInfoObjectMother.Create(parameters: new[] { _parameter }, typeArguments: new[] { _typeParameter });
            _typeArgument            = CustomTypeObjectMother.Create();

            var info = new MethodInstantiationInfo(_genericMethodDefinition, new[] { _typeArgument });

            _instantiation = new MethodInstantiation(info);
        }
        public void SetUp()
        {
            _indexParameter = CustomParameterInfoObjectMother.Create();

            _declaringType    = TypeInstantiationObjectMother.Create();
            _getMethod        = MethodOnTypeInstantiationObjectMother.Create(_declaringType, typeof(GenericType <>).GetMethod("get_Item"));
            _setMethod        = MethodOnTypeInstantiationObjectMother.Create(_declaringType, typeof(GenericType <>).GetMethod("set_Item"));
            _originalProperty = CustomPropertyInfoObjectMother.Create(
                indexParameters: new[] { _indexParameter }, getMethod: _getMethod, setMethod: _setMethod);

            _property = new PropertyOnTypeInstantiation(_declaringType, _originalProperty, _getMethod, _setMethod);
        }
예제 #3
0
        // Creates the property overrides for the proxy
        private void GenerateProxyProperties(Type baseType, TypeBuilder typeBuilder, IEnumerable <PropertyInfo> properties = null)
        {
            var getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Final;

            if (properties == null)
            {
                properties = baseType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            }

            foreach (var prop in properties)
            {
                var attributes = AspectExtensions.GetAttributeContainers(prop.GetCustomAttributes(true), prop.GetCustomAttributeData()).ToArray();

                if (attributes.Length == 0)
                {
                    continue;
                }

                var propBuilder   = typeBuilder.DefineProperty(prop.Name, prop.Attributes, prop.PropertyType, null);
                var getname       = "get_" + prop.Name;
                var setname       = "set_" + prop.Name;
                var accessors     = prop.GetAccessors(true);
                var accessorNames = accessors.Select(x => x.Name);


                if (accessorNames.Contains(getname))
                {
                    MethodBuilder getMBuilder = typeBuilder.DefineMethod(getname, getSetAttr, prop.PropertyType, Type.EmptyTypes);
                    ILGenerator   getIl       = getMBuilder.GetILGenerator();

                    //Getter interception
                    EmitOnPropertyGetting(getIl, baseType, prop.PropertyType, getname, attributes);

                    propBuilder.SetGetMethod(getMBuilder);
                }

                if (accessorNames.Contains(setname))
                {
                    MethodBuilder setMBuilder = typeBuilder.DefineMethod(setname, getSetAttr, null, new Type[] { prop.PropertyType });
                    setMBuilder.DefineParameter(1, ParameterAttributes.None, "value");
                    ILGenerator setIl = setMBuilder.GetILGenerator();

                    //Setter interception
                    var cpi = new CustomParameterInfo {
                        ParameterType = prop.PropertyType
                    };
                    EmitOnPropertySetting(setIl, baseType, prop.PropertyType, setname, attributes, new CustomParameterInfo[] { cpi });

                    propBuilder.SetSetMethod(setMBuilder);
                }
            }
        }
예제 #4
0
        public void SetUp()
        {
            _declaringType  = CustomTypeObjectMother.Create();
            _type           = ReflectionObjectMother.GetSomeType();
            _valueParameter = CustomParameterInfoObjectMother.Create(type: _type);
            var indexParameterType = ReflectionObjectMother.GetSomeOtherType();

            _indexParameter = CustomParameterInfoObjectMother.Create(type: indexParameterType);
            _getMethod      = CustomMethodInfoObjectMother.Create(attributes: MethodAttributes.Public, parameters: new[] { _indexParameter }, returnParameter: _valueParameter);
            _setMethod      = CustomMethodInfoObjectMother.Create(attributes: MethodAttributes.Public, parameters: new[] { _indexParameter, _valueParameter });

            _readOnlyProperty  = CustomPropertyInfoObjectMother.Create(getMethod: _getMethod);
            _writeOnlyProperty = CustomPropertyInfoObjectMother.Create(setMethod: _setMethod);
        }