Exemple #1
0
 private static DynamicTypeInfo EmitMethods <T>(
     this DynamicTypeInfo that,
     IProxyMonitor monitor
     )
 {
     foreach (var method in typeof(T).GetMethods())
     {
         EmitMethod(that, method, monitor);
     }
     return(that);
 }
Exemple #2
0
 private static void EmitMethod(
     DynamicTypeInfo t,
     MethodInfo method,
     IProxyMonitor monitor = null
     )
 {
     var body = EmitMethodSignature(t, method, monitor)
                .EmitBeforeExecuteCall(condition: monitor != null, method: method)
                .EmitConcreteMethodCall(method)
                .EmitAfterExecuteCall(condition: monitor != null, method: method)
                .Ret();
 }
Exemple #3
0
 public static T CreateProxy <T>(
     T concreteInstance,
     IProxyMonitor monitor = null
     )
 {
     return(CreateInstance <T>(
                IL.NewType().Implements <T>()
                .EmitConcreteInstanceSupport <T>()
                .EmitProxyMonitorSupport(condition: monitor != null)
                .EmitMethods <T>(monitor),
                concreteInstance, monitor));
 }
Exemple #4
0
        private static T CreateInstance <T>(
            this DynamicTypeInfo that,
            T concreteInstance,
            IProxyMonitor monitor = null
            )
        {
            var type   = that.AsType;
            var result = (T)Activator.CreateInstance(type);

            var setupConcreteInstance = type.GetMethod("__SetConcreteInstance");

            setupConcreteInstance.Invoke(result, new object[] { concreteInstance });

            if (monitor != null)
            {
                var setupProxyMonitor = type.GetMethod("__SetProxyMonitor");
                setupProxyMonitor.Invoke(result, new object[] { monitor });
            }
            return(result);
        }
Exemple #5
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));
        }