示例#1
0
        // Creates the method overrides for the proxy
        private void GenerateProxyMethods(Type baseType, TypeBuilder builder)
        {
            var methods = baseType.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);

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

                if (attributes.Length == 0 ||
                    method.Name.Contains("get_") ||
                    method.Name.Contains("set_") ||
                    method.Module.Name == "mscorlib.dll")
                {
                    continue;
                }

                var methodAttributes = method.Attributes ^ MethodAttributes.VtableLayoutMask;
                var cpi            = method.GetParameters().ToCustomParameters();
                var parameterTypes = cpi.Select(x => x.ParameterType).ToArray();

                var methodBuilder = builder.DefineMethod(method.Name, methodAttributes, CallingConventions.HasThis, method.ReturnType, parameterTypes);
                var methodIl      = methodBuilder.GetILGenerator();

                EmitOnMethodExecution(methodIl, baseType, method, attributes, cpi);
            }
        }
示例#2
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);
                }
            }
        }