Пример #1
0
        public void ResolveExtensionMethods()
        {
//			Console.WriteLine (" --- Resolve extension");
//			Console.WriteLine ("---Args:");
//			foreach (var arg in arguments)
//				Console.WriteLine (arg);
//			Console.WriteLine ("---GenArgs:");
//			if (genericArguments != null) {
//				foreach (var arg in genericArguments)
//					Console.WriteLine (arg);
//			} else {
//				Console.WriteLine ("<null>");
//			}
            Debug.Assert(originalMethods.Count == methods.Count);
            for (int i = 0; i < originalMethods.Count; i++)
            {
                if (originalMethods[i] is ExtensionMethod)                   // Extension methods are already resolved & instantiated.
                {
                    methods[i] = originalMethods[i];
                }
                else if (!StaticUsage && originalMethods[i].IsExtension && Type != null)
                {
                    methods[i] = new ExtensionMethod(Type, originalMethods[i], genericArguments, arguments);
                }
                else
                {
                    methods[i] = DomMethod.CreateInstantiatedGenericMethod(originalMethods[i], genericArguments, arguments);
                }
            }
//			Console.WriteLine ("-- end resolve extension.");
        }
Пример #2
0
        public IMethod Extends(ProjectDom dom, IType type)
        {
            if (dom == null || type == null || Parameters.Count == 0 || !IsExtension)
            {
                return(null);
            }
            string extensionTableKey = this.HelpUrl + "/" + type.FullName;

            if (Name.StartsWith("ForEachWithIndex"))
            {
                Console.WriteLine("table key:" + extensionTableKey);
            }
            lock (extensionTable) {
                if (extensionTable.ContainsKey(extensionTableKey))
                {
                    return(extensionTable[extensionTableKey]);
                }
                if (type.BaseType != null && type.BaseType.FullName == "System.Array" && Parameters[0].ReturnType.ArrayDimensions > 0)
                {
                    IReturnType elementType = null;
                    foreach (IReturnType returnType in type.BaseTypes)
                    {
                        if (returnType.FullName == "System.Collections.Generic.IList" && returnType.GenericArguments.Count > 0)
                        {
                            elementType = returnType.GenericArguments[0];
                            break;
                        }
                    }
                    if (elementType != null)
                    {
                        IMethod instMethod = DomMethod.CreateInstantiatedGenericMethod(this, new IReturnType[] {}, new IReturnType[] { elementType });
                        instMethod = new ExtensionMethod(type, instMethod, null, null);
                        extensionTable.Add(extensionTableKey, instMethod);
                        return(instMethod);
                    }
                }
                foreach (IType baseType in dom.GetInheritanceTree(type))
                {
                    IMethod instMethod       = DomMethod.CreateInstantiatedGenericMethod(this, new IReturnType[] {}, new IReturnType[] { new DomReturnType(baseType) });
                    string  baseTypeFullName = baseType is InstantiatedType ? ((InstantiatedType)baseType).UninstantiatedType.FullName : baseType.FullName;

                    // compare the generic arguments.
                    if (instMethod.Parameters[0].ReturnType.FullName == baseTypeFullName && Parameters[0].ReturnType.ArrayDimensions == 0 && Parameters[0].ReturnType.PointerNestingLevel == 0)
                    {
                        if (instMethod.Parameters[0].ReturnType.GenericArguments.Count > 0)
                        {
                            InstantiatedType instType = baseType as InstantiatedType;
                            if (instType == null || instType.GenericParameters.Count != instMethod.Parameters[0].ReturnType.GenericArguments.Count)
                            {
                                continue;
                            }
                            bool genericArgumentsAreEqual = true;
                            for (int i = 0; i < instMethod.Parameters[0].ReturnType.GenericArguments.Count; i++)
                            {
                                if (Name.StartsWith("ForEachWithIndex"))
                                {
                                    Console.WriteLine(instMethod.Parameters[0].ReturnType.GenericArguments[i].DecoratedFullName + " --- " + instType.GenericParameters[i].DecoratedFullName);
                                }
                                if (instMethod.Parameters[0].ReturnType.GenericArguments[i].DecoratedFullName != instType.GenericParameters[i].DecoratedFullName)
                                {
                                    genericArgumentsAreEqual = false;
                                    break;
                                }
                            }
                            if (!genericArgumentsAreEqual)
                            {
                                continue;
                            }
                        }

                        //ExtensionMethod result = new ExtensionMethod (baseType, this, null, null);
                        instMethod = new ExtensionMethod(type, instMethod, null, null);
                        extensionTable.Add(extensionTableKey, instMethod);
                        //Console.WriteLine ("ext. method:" + instMethod);
                        return(instMethod);
                    }
                }
//				Console.WriteLine ("null");
                extensionTable.Add(extensionTableKey, null);
                return(null);
            }
        }
Пример #3
0
		public void ResolveExtensionMethods ()
		{
//			Console.WriteLine (" --- Resolve extension");
//			Console.WriteLine ("---Args:");
//			foreach (var arg in arguments)
//				Console.WriteLine (arg);
//			Console.WriteLine ("---GenArgs:");
//			if (genericArguments != null) {
//				foreach (var arg in genericArguments)
//					Console.WriteLine (arg);
//			} else {
//				Console.WriteLine ("<null>");
//			}
			Debug.Assert (originalMethods.Count == methods.Count);
			for (int i = 0; i < originalMethods.Count; i++) {
				if (originalMethods[i] is ExtensionMethod) { // Extension methods are already resolved & instantiated.
					methods[i] = originalMethods[i];
				} else if (!StaticUsage && originalMethods[i].IsExtension && Type != null) {
					methods[i] = new ExtensionMethod (Type, originalMethods[i], genericArguments, arguments);
				} else {
					methods[i] = DomMethod.CreateInstantiatedGenericMethod (originalMethods[i], genericArguments, arguments);
				}
			}
//			Console.WriteLine ("-- end resolve extension.");
		}
Пример #4
0
		public void ExtensionMethodPreserveParameterTest ()
		{
			// build "T MyMethod<T, S> (T a, S b)"
			DomMethod method = new DomMethod ();
			method.Name = "MyMethod";
			method.ReturnType = new DomReturnType ("T");
			method.AddTypeParameter (new TypeParameter ("T"));
			method.AddTypeParameter (new TypeParameter ("S"));
			
			method.Add (new DomParameter (method, "a", new DomReturnType ("T")));
			method.Add (new DomParameter (method, "b", new DomReturnType ("S")));
			
			// extend method
			List<IReturnType> genArgs = new List<IReturnType> ();
			List<IReturnType> args    = new List<IReturnType> ();
			DomType extType = new DomType ("MyType");
			
			ExtensionMethod extMethod = new ExtensionMethod (extType, method, genArgs, args);
			
			// check for MyType MyMethod<S> (S b)
			Assert.AreEqual ("MyType", extMethod.ReturnType.FullName);
			Assert.AreEqual ("S", extMethod.Parameters[0].ReturnType.FullName);
			Assert.AreEqual (1, extMethod.TypeParameters.Count);
			Assert.AreEqual ("S", extMethod.TypeParameters[0].Name);
		}
Пример #5
0
		public void ExtensionMethodTest ()
		{
			// build "T MyMethod<T, S> (this KeyValuePair<T, S> a, S b)"
			DomMethod method = new DomMethod ();
			method.Name = "MyMethod";
			method.ReturnType = new DomReturnType ("T");
			method.AddTypeParameter (new TypeParameter ("T"));
			method.AddTypeParameter (new TypeParameter ("S"));
			
			DomReturnType returnType = new DomReturnType ("KeyValuePair");
			returnType.AddTypeParameter (new DomReturnType ("T"));
			returnType.AddTypeParameter (new DomReturnType ("S"));
			method.Add (new DomParameter (method, "a", returnType));
			method.Add (new DomParameter (method, "b", new DomReturnType ("S")));
			
			// Build extendet type KeyValuePair<int, object>
			DomType type = new DomType ("KeyValuePair");
			type.AddTypeParameter (new TypeParameter ("T"));
			type.AddTypeParameter (new TypeParameter ("S"));
			IType extType = DomType.CreateInstantiatedGenericTypeInternal (type, new IReturnType[] { DomReturnType.Int32, DomReturnType.Object });
			Console.WriteLine (extType);
			
			// extend method
			List<IReturnType> genArgs = new List<IReturnType> ();
			List<IReturnType> args    = new List<IReturnType> ();
			
			ExtensionMethod extMethod = new ExtensionMethod (extType, method, genArgs, args);
			
			Console.WriteLine (extMethod);
			// check 
			Assert.AreEqual (DomReturnType.Int32.FullName, extMethod.ReturnType.FullName);
			Assert.AreEqual (DomReturnType.Object.FullName, extMethod.Parameters[0].ReturnType.FullName);
		}