예제 #1
0
        public void Emit(
            string propertyName,
            Type propertyType,
            Action <DynamicMethodBody> getmethod,
            Action <DynamicMethodBody> setmethod = null
            )
        {
            var property = _dynamicTypeInfoField.TypeBuilder.DefineProperty(
                propertyName,
                PropertyAttributes.None,
                propertyType,
                new Type[] {}
                );

            var getMethodinfo = _dynamicTypeInfoField
                                .WithMethod($"get_{propertyName}")
                                .TurnOnAttributes(MethodAttributes.RTSpecialName)
                                .TurnOnAttributes(MethodAttributes.SpecialName);

            getmethod(getMethodinfo.Returns(propertyType));
            property.SetGetMethod(getMethodinfo.MethodBuilder);

            if (setmethod == null)
            {
                return;
            }
            var setMethodinfo = _dynamicTypeInfoField
                                .WithMethod($"set_{propertyName}")
                                .TurnOnAttributes(MethodAttributes.RTSpecialName)
                                .TurnOnAttributes(MethodAttributes.SpecialName)
                                .WithParameter(propertyType, "value");

            setmethod(setMethodinfo.Returns(typeof(void)));
            property.SetSetMethod(setMethodinfo.MethodBuilder);
        }
예제 #2
0
        public void Emit(
            string propertyName,
            Type propertyType,
            Action <DynamicMethodBody> getmethod,
            Action <DynamicMethodBody> setmethod = null
            )
        {
            PropertyBuilder property = dynamicTypeInfoField.TypeBuilder.DefineProperty(
                propertyName,
                PropertyAttributes.None,
                propertyType,
                new Type[] {}
                );

            DynamicMethodInfo getMethodinfo = dynamicTypeInfoField
                                              .WithMethod(string.Format("get_{0}", propertyName))
                                              .TurnOnAttributes(MethodAttributes.RTSpecialName)
                                              .TurnOnAttributes(MethodAttributes.SpecialName);

            getmethod(getMethodinfo.Returns(propertyType));
            property.SetGetMethod(getMethodinfo.MethodBuilder);

            if (setmethod != null)
            {
                DynamicMethodInfo setMethodinfo = dynamicTypeInfoField
                                                  .WithMethod(string.Format("set_{0}", propertyName))
                                                  .TurnOnAttributes(MethodAttributes.RTSpecialName)
                                                  .TurnOnAttributes(MethodAttributes.SpecialName)
                                                  .WithParameter(propertyType, "value");

                setmethod(setMethodinfo.Returns(typeof(void)));
                property.SetSetMethod(setMethodinfo.MethodBuilder);
            }
        }
예제 #3
0
        private static DynamicMethodBody EmitMethodSignature(DynamicTypeInfo t, MethodInfo method, IProxyMonitor monitor)
        {
            var ilmethod   = t.WithMethod(method.Name);
            var parameters = method.GetParameters();

            foreach (var param in parameters)
            {
                ilmethod.WithParameter(
                    param.ParameterType,
                    param.Name
                    );
            }

            if (monitor != null)
            {
                if (method.ReturnType != typeof(void))
                {
                    ilmethod.WithVariable(method.ReturnType);
                }

                if (parameters.Length > 0)
                {
                    ilmethod.WithVariable(typeof(object[]), "parameters");
                }
            }

            return(ilmethod
                   .Returns(method.ReturnType));
        }