static (DmdConstructorInfo ctor, IList <DmdCustomAttributeTypedArgument> constructorArguments) GetConstructor(DmdType type, SecurityAction action)
        {
            var appDomain          = type.AppDomain;
            var securityActionType = appDomain.GetWellKnownType(DmdWellKnownType.System_Security_Permissions_SecurityAction);
            var ctor = type.GetConstructor(new[] { securityActionType });

            if ((object)ctor != null)
            {
                var ctorArgs = new[] { new DmdCustomAttributeTypedArgument(securityActionType, (int)action) };
                return(ctor, ctorArgs);
            }

            ctor = type.GetConstructor(Array.Empty <DmdType>()) ?? type.GetConstructors().FirstOrDefault();
            return(ctor, null);
        }
示例#2
0
        static DmdConstructorInfo GetConstructor(DmdType proxyType, DmdType targetType)
        {
            if ((object)proxyType == null)
            {
                return(null);
            }
            if (proxyType.IsConstructedGenericType)
            {
                return(null);
            }

            var proxyTypeGenericArgs  = proxyType.GetGenericArguments();
            var targetTypeGenericArgs = targetType.GetGenericArguments();

            if (proxyTypeGenericArgs.Count != targetTypeGenericArgs.Count)
            {
                return(null);
            }

            if (targetTypeGenericArgs.Count != 0)
            {
                proxyType = proxyType.MakeGenericType(targetTypeGenericArgs);
            }
            var ctors = proxyType.GetConstructors(DmdBindingFlags.Public | DmdBindingFlags.NonPublic | DmdBindingFlags.Instance);

            foreach (var ctor in ctors)
            {
                var types = ctor.GetMethodSignature().GetParameterTypes();
                if (types.Count != 1)
                {
                    continue;
                }
                if (!types[0].IsAssignableFrom(targetType))
                {
                    continue;
                }

                return(ctor);
            }
            return(null);
        }