Exemplo n.º 1
0
        private DelegateMap(Type delegateType, Type thisParameter, SimpleParameterInfo[] methodParameters, Type methodReturn)
        {
            if (delegateType == null)
            {
                throw new ArgumentNullException(nameof(delegateType));
            }
            DelegateType = delegateType;

            // delegate info
            var invokeMethod   = GetDelegateInvokeMethod();
            var delegateParams = SimpleParameterInfo.FromParameters(invokeMethod.GetParameters());
            var delegateReturn = invokeMethod.ReturnType;

            // map
            try
            {
                var hasThis = thisParameter != null;
                // Check param count
                if (delegateParams.Length != methodParameters.Length + (hasThis ? 1 : 0))
                {
                    throw new Exception("Invalid parameters count.");
                }

                ParametersMap = new ArgMap[delegateParams.Length];
                for (var i = 0; i < ParametersMap.Length; i++)
                {
                    var methodParam = hasThis
                        ? i == 0
                            ? new SimpleParameterInfo(thisParameter)
                            : methodParameters[i - 1]
                        : methodParameters[i];

                    ParametersMap[i] = new ArgMap(i, delegateParams[i], methodParam);
                }

                RetValMap = new RetValMap(delegateReturn, methodReturn);
            }
            catch (Exception ex)
            {
                throw InvalidSignatureException(ex);
            }
        }
Exemplo n.º 2
0
 public static DelegateMap Constructor(Type delegateType, ConstructorInfo constructor) =>
 new DelegateMap(delegateType, null, SimpleParameterInfo.FromParameters(constructor.GetParameters()),
                 constructor.DeclaringType);
Exemplo n.º 3
0
 public static DelegateMap Method(Type delegateType, MethodInfo methodInfo) =>
 new DelegateMap(delegateType, GetThisParameter(methodInfo), SimpleParameterInfo.FromParameters(methodInfo.GetParameters()), methodInfo.ReturnType);