Ejemplo n.º 1
0
		/// <inheritdoc />
		public object CreateProxy (Type baseType, Type[] implementedInterfaces, object[] constructorArguments)
		{
			if (baseType.IsInterface) {
				// TODO: should Moq.Core do this work? It's the same for 
				// Castle and LinFu and presumably other (future?) libraries

				var fixedInterfaces = new Type[implementedInterfaces.Length + 1];
				fixedInterfaces[0] = baseType;
				implementedInterfaces.CopyTo (fixedInterfaces, 1);
				implementedInterfaces = fixedInterfaces;
				baseType = typeof (object);
			}

			if (!implementedInterfaces.Contains(typeof(IProxy))) {
				var fixedInterfaces = new Type[implementedInterfaces.Length + 1];
				fixedInterfaces[0] = typeof(IProxy);
				implementedInterfaces.CopyTo (fixedInterfaces, 1);
				implementedInterfaces = fixedInterfaces;
			}

			// TODO: the proxy factory should automatically detect requests to proxy 
			// delegates and generate an interface on the fly for them, without Moq 
			// having to know about it at all.

			return generator.CreateClassProxy (baseType, implementedInterfaces, proxyOptions, constructorArguments, new Interceptor ());
		}
Ejemplo n.º 2
0
		public object CreateProxy (Type baseType, Type[] implementedInterfaces, object[] constructorArguments)
		{
			if (baseType.IsInterface) {
				// TODO: should Moq.Core do this work? It's the same for 
				// Castle and LinFu and presumably other (future?) libraries

				var fixedInterfaces = new Type[implementedInterfaces.Length + 1];
				fixedInterfaces[0] = baseType;
				implementedInterfaces.CopyTo (fixedInterfaces, 1);
				implementedInterfaces = fixedInterfaces;
				baseType = typeof (object);
			}

			// TODO: this code is duplicated exactly from the Castle proxy factory.
			if (!implementedInterfaces.Contains (typeof (IProxy))) {
				var fixedInterfaces = new Type[implementedInterfaces.Length + 1];
				fixedInterfaces[0] = typeof (IProxy);
				implementedInterfaces.CopyTo (fixedInterfaces, 1);
				implementedInterfaces = fixedInterfaces;
			}

			// TODO: the proxy factory should automatically detect requests to proxy 
			// delegates and generate an interface on the fly for them, without Moq 
			// having to know about it at all.

			// NOTE: LinFu doesn't support passing ctor args??!?!?!
			if (constructorArguments.Length != 0)
				throw new NotSupportedException ();

			return factory.CreateProxy(baseType, new Interceptor (), implementedInterfaces);
		}
Ejemplo n.º 3
0
		/// <summary>
		/// Creates the func delegate, that handles  call with N arguments and call Action<object[]> with this arguments
		/// </summary>
		/// <returns>The converter to arguments array action.</returns>
		/// <param name="action">Action.</param>
		/// <param name="argTypes">Argument types.</param>
		public static Delegate CreateConverterToArgsArrayFunc(Func<object[], object> func,Type returnType, Type[] argTypes)
		{
			Type t = null;
			switch (argTypes.Length){
				case 0:  t = typeof(FuncCallConverterSubFactory<>); break;
				case 1:	 t = typeof(FuncCallConverterSubFactory<,>); break;
				case 2:	 t = typeof(FuncCallConverterSubFactory<,,>); break;
				case 3:	 t = typeof(FuncCallConverterSubFactory<,,,>); break;
				case 4:	 t = typeof(FuncCallConverterSubFactory<,,,,>); break;
				case 5:	 t = typeof(FuncCallConverterSubFactory<,,,,,>); break;
				case 6:	 t = typeof(FuncCallConverterSubFactory<,,,,,,>); break;
				case 7:	 t = typeof(FuncCallConverterSubFactory<,,,,,,,>); break;
				case 8:	 t = typeof(FuncCallConverterSubFactory<,,,,,,,,>); break;
				case 9:	 t = typeof(FuncCallConverterSubFactory<,,,,,,,,,>); break;
				case 10: t = typeof(FuncCallConverterSubFactory<,,,,,,,,,,>); break;
				case 11: t = typeof(FuncCallConverterSubFactory<,,,,,,,,,,,>); break;
				case 12: t = typeof(FuncCallConverterSubFactory<,,,,,,,,,,,,>); break;
                default:
                    throw new ArgumentException("Arguments number should be less or equal 12");
//				case 13: t = typeof(FuncCallConverterSubFactory<,,,,,,,,,,,,,>); break;
//				case 14: t = typeof(FuncCallConverterSubFactory<,,,,,,,,,,,,,,>); break;
//				case 15: t = typeof(FuncCallConverterSubFactory<,,,,,,,,,,,,,,,>); break;
//				case 16: t = typeof(FuncCallConverterSubFactory<,,,,,,,,,,,,,,,,>); break;
			}

			var FuncTypes = new Type[argTypes.Length + 1];
			argTypes.CopyTo (FuncTypes, 0);
			FuncTypes [argTypes.Length] = returnType;  

			var gt = t.MakeGenericType (FuncTypes);
			var gen = Activator.CreateInstance (gt) as IFuncCallConverterSubFactory;
			return gen.GetFuncConverter (func);
		}
Ejemplo n.º 4
0
		internal MethodOnTypeBuilderInst (MethodOnTypeBuilderInst gmd, Type[] typeArguments)
		{
			this.instantiation = gmd.instantiation;
			this.mb = gmd.mb;
			this.method_arguments = new Type [typeArguments.Length];
			typeArguments.CopyTo (this.method_arguments, 0);
			this.generic_method_definition = gmd;
		}
		internal MethodOnTypeBuilderInst (MethodInfo method, Type[] typeArguments)
		{
			this.instantiation = method.DeclaringType;
			this.base_method = ExtractBaseMethod (method);
			this.method_arguments = new Type [typeArguments.Length];
			typeArguments.CopyTo (this.method_arguments, 0);
			if (base_method != method)
				this.generic_method_definition = method;
		}
Ejemplo n.º 6
0
        internal static Type GetGeneratorFactoryTarget(Type[] parameterTypes) {
            Type[] typeArgs = new Type[parameterTypes.Length + 2];
            typeArgs[0] = typeof(DebugFrame);
            parameterTypes.CopyTo(typeArgs, 1);
            typeArgs[parameterTypes.Length + 1] = typeof(IEnumerator);

            if (typeArgs.Length <= 16) {
                return Ast.GetFuncType(typeArgs);
            } else {
                return DelegateHelpers.MakeNewCustomDelegateType(typeArgs);
            }
        }
        private Type[] BuildAdditionalTypeArrayForProxyType(Type[] additionalTypes)
        {
            var allAdditionalTypes = new Type[additionalTypes.Length + 1];

            allAdditionalTypes[0] = typeof(IMockObject);

            if (additionalTypes.Length > 0)
            {
                additionalTypes.CopyTo(allAdditionalTypes, 1);
            }

            return allAdditionalTypes;
        }
Ejemplo n.º 8
0
        internal static Type GetGeneratorFactoryTarget(Type[] parameterTypes)
        {
            Type[] typeArgs = new Type[parameterTypes.Length + 2];
            typeArgs[0] = typeof(DebugFrame);
            parameterTypes.CopyTo(typeArgs, 1);
            typeArgs[parameterTypes.Length + 1] = typeof(IEnumerator);

            if (typeArgs.Length <= 16) {
                return Ast.GetFuncType(typeArgs);
            } else {
            #if FEATURE_REFEMIT
                return DelegateHelpers.MakeNewCustomDelegateType(typeArgs);
            #else
                throw new NotSupportedException("Signature not supported on this platform.");
            #endif
            }
        }
 private static Type[] CombineAttributeIdEnumDefiningTypes(Type[] attributeIdEnumDefiningTypes, Type[] recordSpecificAttributeIdEnumDefiningTypes)
 {
     Type[] allAttributeIdEnumDefiningTypes;
     if (attributeIdEnumDefiningTypes == null) {
         attributeIdEnumDefiningTypes = new Type[0];
     }
     allAttributeIdEnumDefiningTypes = new Type[1 + attributeIdEnumDefiningTypes.Length
         + recordSpecificAttributeIdEnumDefiningTypes.Length];
     //
     allAttributeIdEnumDefiningTypes[0] = typeof(AttributeIds.UniversalAttributeId);
     attributeIdEnumDefiningTypes.CopyTo(allAttributeIdEnumDefiningTypes, 1);
     recordSpecificAttributeIdEnumDefiningTypes.CopyTo(allAttributeIdEnumDefiningTypes,
         1 + attributeIdEnumDefiningTypes.Length);
     return allAttributeIdEnumDefiningTypes;
 }
Ejemplo n.º 10
0
		protected Type[] CollectInterfaces(Type[] interfaces, ComponentModel model)
		{
			var modelInterfaces = model.Implementation.FindInterfaces(EmptyTypeFilter, model.Service);

			if (interfaces == null || interfaces.Length == 0)
			{
				interfaces = modelInterfaces;
			}
			else if (modelInterfaces.Length > 0)
			{
				var allInterfaces = new Type[interfaces.Length + modelInterfaces.Length];
				interfaces.CopyTo(allInterfaces, 0);
				modelInterfaces.CopyTo(allInterfaces, interfaces.Length);
				interfaces = allInterfaces;
			}

			return interfaces;
		}
Ejemplo n.º 11
0
        public MethodBase ResolvePlug(Type aTargetType, List<Type> aImpls, MethodBase aMethod, Type[] aParamTypes)
        {
            //TODO: This method is "reversed" from old - remember that when porting
            MethodBase xResult = null;

            // Setup param types for search
            Type[] xParamTypes;
            if (aMethod.IsStatic)
            {
                xParamTypes = aParamTypes;
            }
            else
            {
                // If its an instance method, we have to add this to the ParamTypes to search
                xParamTypes = new Type[aParamTypes.Length + 1];
                if (aParamTypes.Length > 0)
                {
                    aParamTypes.CopyTo(xParamTypes, 1);
                }
                xParamTypes[0] = aTargetType;
            }

            PlugMethodAttribute xAttrib = null;
            foreach (var xImpl in aImpls)
            {
                // TODO: cleanup this loop, next statement shouldnt be neccessary
                if (xResult != null)
                {
                    break;
                }
                // Plugs methods must be static, and public
                // Search for non signature matches first since signature searches are slower
                xResult = xImpl.GetMethod(aMethod.Name, BindingFlags.Static | BindingFlags.Public
                  , null, xParamTypes, null);
                if (xResult == null && aMethod.Name == ".ctor")
                {
                    xResult = xImpl.GetMethod("Ctor", BindingFlags.Static | BindingFlags.Public
                      , null, xParamTypes, null);
                }
                if (xResult == null && aMethod.Name == ".cctor")
                {
                    xResult = xImpl.GetMethod("CCtor", BindingFlags.Static | BindingFlags.Public
                      , null, xParamTypes, null);
                }

                if (xResult == null)
                {
                    // Search by signature
                    foreach (var xSigMethod in xImpl.GetMethods(BindingFlags.Static | BindingFlags.Public))
                    {
                        // TODO: Only allow one, but this code for now takes the last one
                        // if there is more than one
                        xAttrib = null;
                        foreach (PlugMethodAttribute x in xSigMethod.GetCustomAttributes(typeof(PlugMethodAttribute), false))
                        {
                            xAttrib = x;
                        }

                        if (xAttrib != null && (xAttrib.IsWildcard && !xAttrib.WildcardMatchParameters))
                        {
                            MethodBase xTargetMethod = null;
                            if (String.Compare(xSigMethod.Name, "Ctor", true) == 0 ||
                               String.Compare(xSigMethod.Name, "Cctor", true) == 0)
                            {
                                xTargetMethod = aTargetType.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).SingleOrDefault();
                            }
                            else
                            {
                                xTargetMethod = (from item in aTargetType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
                                                 where item.Name == xSigMethod.Name
                                                 select item).SingleOrDefault();
                            }
                            if (xTargetMethod == aMethod)
                            {
                                xResult = xSigMethod;
                            }
                        }
                        else
                        {

                            var xParams = xSigMethod.GetParameters();
                            //TODO: Static method plugs dont seem to be separated
                            // from instance ones, so the only way seems to be to try
                            // to match instance first, and if no match try static.
                            // I really don't like this and feel we need to find
                            // an explicit way to determine or mark the method
                            // implementations.
                            //
                            // Plug implementations take "this" as first argument
                            // so when matching we don't include it in the search
                            Type[] xTypesInst = null;
                            var xActualParamCount = xParams.Length;
                            foreach (var xParam in xParams)
                            {
                                if (xParam.GetCustomAttributes(typeof(FieldAccessAttribute), false).Length > 0)
                                {
                                    xActualParamCount--;
                                }
                            }
                            Type[] xTypesStatic = new Type[xActualParamCount];
                            // If 0 params, has to be a static plug so we skip
                            // any copying and leave xTypesInst = null
                            // If 1 params, xTypesInst must be converted to Type[0]
                            if (xActualParamCount == 1)
                            {
                                xTypesInst = new Type[0];

                                var xReplaceType = xParams[0].GetCustomAttributes(typeof(FieldTypeAttribute), false);
                                if (xReplaceType.Length == 1)
                                    xTypesStatic[0] = Type.GetType(((FieldTypeAttribute)xReplaceType[0]).Name, true);
                                else
                                    xTypesStatic[0] = xParams[0].ParameterType;
                            }
                            else if (xActualParamCount > 1)
                            {
                                xTypesInst = new Type[xActualParamCount - 1];
                                var xCurIdx = 0;
                                foreach (var xParam in xParams.Skip(1))
                                {
                                    if (xParam.GetCustomAttributes(typeof(FieldAccessAttribute), false).Length > 0)
                                    {
                                        continue;
                                    }

                                    var xReplaceType = xParam.GetCustomAttributes(typeof(FieldTypeAttribute), false);
                                    if (xReplaceType.Length == 1)
                                        xTypesInst[xCurIdx] = Type.GetType(((FieldTypeAttribute)xReplaceType[0]).Name, true);
                                    else
                                        xTypesInst[xCurIdx] = xParam.ParameterType;

                                    xCurIdx++;
                                }
                                xCurIdx = 0;
                                foreach (var xParam in xParams)
                                {
                                    if (xParam.GetCustomAttributes(typeof(FieldAccessAttribute), false).Length > 0)
                                    {
                                        xCurIdx++;
                                        continue;
                                    }
                                    if (xCurIdx >= xTypesStatic.Length)
                                    {
                                        break;
                                    }
                                    xTypesStatic[xCurIdx] = xParam.ParameterType;
                                    xCurIdx++;
                                }
                            }
                            SysReflection.MethodBase xTargetMethod = null;
                            // TODO: In future make rule that all ctor plugs are called
                            // ctor by name, or use a new attrib
                            //TODO: Document all the plug stuff in a document on website
                            //TODO: To make inclusion of plugs easy, we can make a plugs master
                            // that references the other default plugs so user exes only
                            // need to reference that one.
                            // TODO: Skip FieldAccessAttribute if in impl
                            if (xTypesInst != null)
                            {
                                if (string.Compare(xSigMethod.Name, "ctor", true) == 0)
                                {
                                    xTargetMethod = aTargetType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, xTypesInst, null);
                                }
                                else
                                {
                                    xTargetMethod = aTargetType.GetMethod(xSigMethod.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, xTypesInst, null);
                                }
                            }
                            // Not an instance method, try static
                            if (xTargetMethod == null)
                            {
                                if (string.Compare(xSigMethod.Name, "cctor", true) == 0
                                  || string.Compare(xSigMethod.Name, "ctor", true) == 0)
                                {
                                    xTargetMethod = aTargetType.GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, xTypesStatic, null);
                                }
                                else
                                {
                                    xTargetMethod = aTargetType.GetMethod(xSigMethod.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, xTypesStatic, null);
                                }
                            }
                            if (xTargetMethod == aMethod)
                            {
                                xResult = xSigMethod;
                                break;
                            }
                            if (xAttrib != null && xAttrib.Signature != null)
                            {
                                var xName = DataMember.FilterStringForIncorrectChars(LabelName.GenerateFullName(aMethod));
                                if (string.Compare(xName, xAttrib.Signature, true) == 0)
                                {
                                    xResult = xSigMethod;
                                    break;
                                }
                            }
                            xAttrib = null;
                        }
                    }
                }
                else
                {
                    // check if signatur is equal
                    var xResPara = xResult.GetParameters();
                    var xAMethodPara = aMethod.GetParameters();
                    if (aMethod.IsStatic)
                    {
                        if (xResPara.Length != xAMethodPara.Length)
                            return null;
                    }
                    else
                    {
                        if (xResPara.Length - 1 != xAMethodPara.Length)
                            return null;
                    }
                    for (int i = 0; i < xAMethodPara.Length; i++)
                    {
                        int correctIndex = aMethod.IsStatic ? i : i + 1;
                        if (xResPara[correctIndex].ParameterType != xAMethodPara[i].ParameterType)
                            return null;
                    }
                    if (xResult.Name == "Ctor" && aMethod.Name == ".ctor")
                    {
                    }
                    else if (xResult.Name == "CCtor" && aMethod.Name == ".cctor")
                    {
                    }
                    else if (xResult.Name != aMethod.Name)
                        return null;
                }
            }
            if (xResult == null)
                return null;

            // If we found a matching method, check for attributes
            // that might disable it.
            //TODO: For signature ones, we could cache the attrib. Thats
            // why we check for null here
            if (xAttrib == null)
            {
                // TODO: Only allow one, but this code for now takes the last one
                // if there is more than one
                foreach (PlugMethodAttribute x in xResult.GetCustomAttributes(typeof(PlugMethodAttribute), false))
                {
                    xAttrib = x;
                }
            }

            // See if we need to disable this plug
            if (xAttrib != null)
            {
                if (!xAttrib.Enabled)
                {
                    //xResult = null;
                    return null;
                }
                else if (xAttrib.IsMonoOnly)
                {
                    //TODO: Check this against build options
                    //TODO: Two exclusive IsOnly's dont make sense
                    // refactor these as a positive rather than negative
                    // Same thing at type plug level
                    //xResult = null;
                    return null;
                }
                //else if (xAttrib.Signature != null) {
                //  var xName = DataMember.FilterStringForIncorrectChars(MethodInfoLabelGenerator.GenerateFullName(xResult));
                //  if (string.Compare(xName, xAttrib.Signature, true) != 0) {
                //    xResult = null;
                //  }
                //}
            }

            InlineAttribute xInlineAttrib = null;
            foreach (InlineAttribute inli in xResult.GetCustomAttributes(typeof(InlineAttribute), false))
            {
                xInlineAttrib = inli;
            }

            if (xInlineAttrib == null)
            {
                if (Queue != null)
                {
                    CompilerHelpers.Debug("Queueing Plug:", xResult.DeclaringType, "::", xResult.Name);
                    Queue(xResult, null, "Plug Method");
                }
            }

            //if (xAttrib != null && xAttrib.Signature != null)
            //{
            //    var xTargetMethods = aTargetType.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            //    //System_Void__Indy_IL2CPU_Assembler_Assembler__cctor__
            //    //If signature exists, the search is slow. Signatures
            //    //are infrequent though, so for now we just go slow method
            //    //and have not optimized or cached this info. When we
            //    //redo the plugs, we can fix this.
            //    bool xEnabled=true;
            //    foreach (var xTargetMethod in xTargetMethods)
            //    {
            //        string sName = DataMember.FilterStringForIncorrectChars(MethodInfoLabelGenerator.GenerateFullName(xTargetMethod));
            //        if (string.Compare(sName, xAttrib.Signature, true) == 0)
            //        {
            //            //uint xUID = QueueMethod(xPlugImpl.Plug, "Plug", xMethod, true);
            //            //mMethodPlugs.Add(xTargetMethod, new PlugInfo(xUID, xAttrib.Assembler));
            //            // Mark as disabled, because we already handled it
            //            xEnabled = false;
            //            break;
            //        }
            //    }
            //    // if still enabled, we didn't find our method
            //    if (xEnabled)
            //    {
            //        // todo: more precise error: imagine having a 100K line project, and this error happens...
            //        throw new Exception("Plug target method not found.");
            //    }
            //}
            return xResult;
        }
Ejemplo n.º 12
0
		ParametersCollection (IParameterData [] parameters, Type [] types, MethodBase method, bool hasParams)
		{
			this.parameters = parameters;
			this.types = types;
			has_arglist = (method.CallingConvention & CallingConventions.VarArgs) != 0;
			if (has_arglist) {
				this.parameters = new IParameterData [parameters.Length + 1];
				parameters.CopyTo (this.parameters, 0);
				this.parameters [parameters.Length] = new ArglistParameter (Location.Null);
				this.types = new Type [types.Length + 1];
				types.CopyTo (this.types, 0);
				this.types [types.Length] = TypeManager.arg_iterator_type;
			}
			has_params = hasParams;
		}
Ejemplo n.º 13
0
        IMessager CreateMessager(MethodInfo methodInfo, Type[] parameterTypes)
        {
            Type actionType;
            Type messagerType;
            Type[] genericArguments = new Type[parameterTypes.Length + 1];
            genericArguments[0] = methodInfo.DeclaringType;
            parameterTypes.CopyTo(genericArguments, 1);

            if (parameterTypes.Length == 0)
            {
                actionType = typeof(Action<>).MakeGenericType(genericArguments);
                messagerType = typeof(Messager<>).MakeGenericType(genericArguments);

            }
            else
            {
                actionType = typeof(Action<,>).MakeGenericType(genericArguments);
                messagerType = typeof(Messager<,>).MakeGenericType(genericArguments);
            }

            Delegate action = Delegate.CreateDelegate(actionType, methodInfo);
            return (IMessager)Activator.CreateInstance(messagerType, action);
        }
Ejemplo n.º 14
0
        public List<PropertyNode> ReadProperties(Type type, Type[] parents, AssemblyNode assembly, string version)
        {
            var newParents = new Type[parents.Length + 1];
            parents.CopyTo(newParents, 0);
            newParents[parents.Length] = type;

            return type.GetProperties()
                .Where(propertyInfo => !IsIgnored(propertyInfo))
                .Select(propertyInfo => ReadProperty(propertyInfo, newParents, assembly, version))
                .ToList();
        }