Esempio n. 1
0
        /// <summary>
        /// Converts WinMD type <paramref name="et"/> to a CLR type. Returns <c>null</c>
        /// if it's not a CLR compatible WinMD type.
        /// </summary>
        /// <param name="module">Owner module or <c>null</c></param>
        /// <param name="et">Type</param>
        /// <returns></returns>
        public static ExportedType ToCLR(ModuleDef module, ExportedType et)
        {
            if (et == null)
            {
                return(null);
            }
            var defAsm = et.DefinitionAssembly;

            if (defAsm == null || !defAsm.IsContentTypeWindowsRuntime)
            {
                return(null);
            }
            if (et.DeclaringType != null)
            {
                return(null);
            }

            if (!winMDToCLR.TryGetValue(new ClassName(et.TypeNamespace, et.TypeName), out var pc))
            {
                return(null);
            }

            return(new ExportedTypeUser(module, 0, pc.ClrClass.Namespace, pc.ClrClass.Name, et.Attributes, CreateAssembly(module, pc.ContractAssembly)));
        }
		/// <summary>
		/// Returns the assembly where this type is defined
		/// </summary>
		/// <param name="exportedType">The <c>ExportedType</c></param>
		/// <returns>A <see cref="IAssembly"/> or <c>null</c> if none found</returns>
		public static IAssembly DefinitionAssembly(ExportedType exportedType) {
			return new FullNameCreator().GetDefinitionAssembly(exportedType);
		}
		/// <summary>
		/// Returns the assembly qualified full name of a <see cref="ExportedType"/>
		/// </summary>
		/// <param name="exportedType">The <c>ExportedType</c></param>
		/// <returns>The assembly qualified full name</returns>
		public static string AssemblyQualifiedName(ExportedType exportedType) {
			return AssemblyQualifiedName(exportedType, null);
		}
		/// <summary>
		/// Returns the full name of a <see cref="ExportedType"/>
		/// </summary>
		/// <param name="exportedType">The <c>ExportedType</c></param>
		/// <param name="isReflection">Set if output should be compatible with reflection</param>
		/// <returns>The full name</returns>
		public static string FullName(ExportedType exportedType, bool isReflection) {
			return FullName(exportedType, isReflection, null);
		}
		ModuleDef GetOwnerModule(ExportedType exportedType) {
			if (exportedType == null)
				return null;
			return exportedType.Module;
		}
		ITypeDefOrRef GetScopeType(ExportedType exportedType) {
			return null;
		}
		void CreateName(ExportedType exportedType) {
			if (exportedType == null) {
				sb.Append(NULLVALUE);
				return;
			}
			AddName(exportedType.TypeName);
		}
		void CreateAssemblyQualifiedName(ExportedType exportedType) {
			if (exportedType == null) {
				sb.Append(NULLVALUE);
				return;
			}
			if (!recursionCounter.Increment()) {
				sb.Append(RECURSION_ERROR_RESULT_STRING);
				return;
			}

			CreateFullName(exportedType);
			if (MustUseAssemblyName(exportedType))
				AddAssemblyName(GetDefinitionAssembly(exportedType));

			recursionCounter.Decrement();
		}
Esempio n. 9
0
 static ExportedType FindExportedType(AssemblyDef asm, ExportedType et)
 {
     foreach (var mod in asm.Modules.GetSafeEnumerable()) {
         foreach (var et2 in mod.ExportedTypes.GetSafeEnumerable()) {
             if (new SigComparer(SigComparerOptions.DontCompareTypeScope).Equals(et, et2))
                 return et2;
         }
     }
     return null;
 }
Esempio n. 10
0
 void Find(ExportedType et)
 {
     if (et == null)
         return;
     // The type might've been moved, so always resolve it instead of using DefinitionAssembly
     var td = et.Resolve(assembly.ModuleDefinition);
     if (td == null)
         Find(et.DefinitionAssembly);
     else
         Find(td.DefinitionAssembly ?? et.DefinitionAssembly);
 }
Esempio n. 11
0
 /// <summary>
 /// Returns the full name of a <see cref="ExportedType"/>
 /// </summary>
 /// <param name="exportedType">The <c>ExportedType</c></param>
 /// <param name="isReflection">Set if output should be compatible with reflection</param>
 /// <param name="helper">Helps print the name</param>
 /// <param name="sb">String builder to use or null</param>
 /// <returns>The full name</returns>
 public static StringBuilder FullNameSB(ExportedType exportedType, bool isReflection, IFullNameCreatorHelper helper, StringBuilder sb)
 {
     var fnc = new FullNameCreator(isReflection, helper, sb);
     fnc.CreateFullName(exportedType);
     return fnc.sb ?? new StringBuilder();
 }
Esempio n. 12
0
 /// <summary>
 /// Returns the full name of a <see cref="ExportedType"/>
 /// </summary>
 /// <param name="exportedType">The <c>ExportedType</c></param>
 /// <param name="isReflection">Set if output should be compatible with reflection</param>
 /// <param name="helper">Helps print the name</param>
 /// <param name="sb">String builder to use or null</param>
 /// <returns>The full name</returns>
 public static string FullName(ExportedType exportedType, bool isReflection, IFullNameCreatorHelper helper, StringBuilder sb)
 {
     return FullNameSB(exportedType, isReflection, helper, sb).ToString();
 }
Esempio n. 13
0
 /// <summary>
 /// Returns the assembly qualified full name of a <see cref="ExportedType"/>
 /// </summary>
 /// <param name="exportedType">The <c>ExportedType</c></param>
 /// <param name="helper">Helps print the name</param>
 /// <param name="sb">String builder to use or null</param>
 /// <returns>The assembly qualified full name</returns>
 public static StringBuilder AssemblyQualifiedNameSB(ExportedType exportedType, IFullNameCreatorHelper helper, StringBuilder sb)
 {
     var fnc = new FullNameCreator(true, helper, sb);
     fnc.CreateAssemblyQualifiedName(exportedType);
     return fnc.sb ?? new StringBuilder();
 }
Esempio n. 14
0
 /// <summary>
 /// Returns the assembly qualified full name of a <see cref="ExportedType"/>
 /// </summary>
 /// <param name="exportedType">The <c>ExportedType</c></param>
 /// <param name="helper">Helps print the name</param>
 /// <param name="sb">String builder to use or null</param>
 /// <returns>The assembly qualified full name</returns>
 public static string AssemblyQualifiedName(ExportedType exportedType, IFullNameCreatorHelper helper, StringBuilder sb)
 {
     return AssemblyQualifiedNameSB(exportedType, helper, sb).ToString();
 }
Esempio n. 15
0
 /// <summary>
 /// Returns the namespace of a <see cref="ExportedType"/>
 /// </summary>
 /// <param name="exportedType">The <c>ExportedType</c></param>
 /// <param name="isReflection">Set if output should be compatible with reflection</param>
 /// <param name="sb">String builder to use or null</param>
 /// <returns>The namespace</returns>
 public static StringBuilder NamespaceSB(ExportedType exportedType, bool isReflection, StringBuilder sb)
 {
     var fnc = new FullNameCreator(isReflection, null, sb);
     fnc.CreateNamespace(exportedType);
     return fnc.sb ?? new StringBuilder();
 }
		/// <summary>
		/// Gets the scope
		/// </summary>
		/// <param name="exportedType">The <c>ExportedType</c></param>
		/// <returns>The <see cref="IScope"/> or <c>null</c> if none found</returns>
		public static IScope Scope(ExportedType exportedType) {
			return new FullNameCreator().GetScope(exportedType);
		}
Esempio n. 17
0
 /// <summary>
 /// Returns the namespace of a <see cref="ExportedType"/>
 /// </summary>
 /// <param name="exportedType">The <c>ExportedType</c></param>
 /// <param name="isReflection">Set if output should be compatible with reflection</param>
 /// <param name="sb">String builder to use or null</param>
 /// <returns>The namespace</returns>
 public static string Namespace(ExportedType exportedType, bool isReflection, StringBuilder sb)
 {
     return NamespaceSB(exportedType, isReflection, sb).ToString();
 }
Esempio n. 18
0
        static TypeDef Resolve(ModuleDef sourceModule, ExportedType et)
        {
            for (int i = 0; i < MAX_LOOP_ITERS; i++) {
                if (et == null || et.module == null)
                    break;
                var resolver = et.module.Context.AssemblyResolver;
                var etAsm = resolver.Resolve(et.DefinitionAssembly, sourceModule ?? et.module);
                if (etAsm == null)
                    break;

                var td = etAsm.Find(et.FullName, false);
                if (td != null)
                    return td;

                et = FindExportedType(etAsm, et);
            }

            return null;
        }
		void CreateFullName(ExportedType exportedType) {
			if (exportedType == null) {
				sb.Append(NULLVALUE);
				return;
			}
			if (!recursionCounter.Increment()) {
				sb.Append(RECURSION_ERROR_RESULT_STRING);
				return;
			}

			var declaringExportedType = exportedType.Implementation as ExportedType;
			if (declaringExportedType != null) {
				CreateFullName(declaringExportedType);
				AddNestedTypeSeparator();
			}

			if (AddNamespace(exportedType.TypeNamespace))
				sb.Append('.');
			AddName(exportedType.TypeName);

			recursionCounter.Decrement();
		}
Esempio n. 20
0
        public static TextTokenKind GetTextTokenType(ExportedType et)
        {
            if (et == null)
                return TextTokenKind.Text;

            return GetTextTokenType(et.ToTypeRef());
        }
		IAssembly GetDefinitionAssembly(ExportedType exportedType) {
			if (exportedType == null)
				return null;
			if (!recursionCounter.Increment())
				return null;
			IAssembly result;
			ExportedType et;
			AssemblyRef asmRef;

			var scope = exportedType.Implementation;
			if ((et = scope as ExportedType) != null)
				result = GetDefinitionAssembly(et);
			else if ((asmRef = scope as AssemblyRef) != null)
				result = asmRef;
			else if (scope is FileDef) {
				var ownerModule = GetOwnerModule(exportedType);
				result = ownerModule == null ? null : ownerModule.Assembly;
			}
			else
				result = null;

			recursionCounter.Decrement();
			return result;
		}
Esempio n. 22
0
		/// <summary>
		/// Checks whether <paramref name="type"/> contains a <see cref="GenericVar"/> or a
		/// <see cref="GenericMVar"/>.
		/// </summary>
		/// <param name="type">Type</param>
		/// <returns><c>true</c> if <paramref name="type"/> contains a <see cref="GenericVar"/> or a
		/// <see cref="GenericMVar"/>.</returns>
		public static bool ContainsGenericParameter(ExportedType type) {
			return new TypeHelper().ContainsGenericParameterInternal(type);
		}
		IScope GetScope(ExportedType exportedType) {
			if (exportedType == null)
				return null;
			if (!recursionCounter.Increment())
				return null;
			IScope result;
			ExportedType et;
			AssemblyRef asmRef;
			FileDef file;

			var scope = exportedType.Implementation;
			if ((et = scope as ExportedType) != null)
				result = GetScope(et);
			else if ((asmRef = scope as AssemblyRef) != null)
				result = asmRef;
			else if ((file = scope as FileDef) != null) {
				var ownerModule = GetOwnerModule(exportedType);
				//TODO: Not all modules' names are equal to the name in FileDef.Name
				var modRef = new ModuleRefUser(ownerModule, file.Name);
				if (ownerModule != null)
					ownerModule.UpdateRowId(modRef);
				result = modRef;
			}
			else
				result = null;

			recursionCounter.Decrement();
			return result;
		}
Esempio n. 24
0
		bool ContainsGenericParameterInternal(ExportedType type) {
			return false;
		}
		/// <summary>
		/// Returns the name of a <see cref="ExportedType"/>
		/// </summary>
		/// <param name="exportedType">The <c>ExportedType</c></param>
		/// <param name="isReflection">Set if output should be compatible with reflection</param>
		/// <returns>The name</returns>
		public static string Name(ExportedType exportedType, bool isReflection) {
			var fnc = new FullNameCreator(isReflection, null);
			fnc.CreateName(exportedType);
			return fnc.Result;
		}
Esempio n. 26
0
 bool ContainsGenericParameterInternal(ExportedType type) => false;
		/// <summary>
		/// Returns the full name of a <see cref="ExportedType"/>
		/// </summary>
		/// <param name="exportedType">The <c>ExportedType</c></param>
		/// <param name="isReflection">Set if output should be compatible with reflection</param>
		/// <param name="helper">Helps print the name</param>
		/// <returns>The full name</returns>
		public static string FullName(ExportedType exportedType, bool isReflection, IFullNameCreatorHelper helper) {
			var fnc = new FullNameCreator(isReflection, helper);
			fnc.CreateFullName(exportedType);
			return fnc.Result;
		}
Esempio n. 28
0
 void Add(ExportedType et)
 {
     if (et == null || exportedTypes.ContainsKey(et))
         return;
     if (et.Module != validModule)
         return;
     exportedTypes[et] = true;
     Add(et.CustomAttributes);
     Push(et.Implementation);
 }
		/// <summary>
		/// Returns the assembly qualified full name of a <see cref="ExportedType"/>
		/// </summary>
		/// <param name="exportedType">The <c>ExportedType</c></param>
		/// <param name="helper">Helps print the name</param>
		/// <returns>The assembly qualified full name</returns>
		public static string AssemblyQualifiedName(ExportedType exportedType, IFullNameCreatorHelper helper) {
			var fnc = new FullNameCreator(true, helper);
			fnc.CreateAssemblyQualifiedName(exportedType);
			return fnc.Result;
		}
Esempio n. 30
0
 /// <summary>
 /// Checks whether <paramref name="type"/> contains a <see cref="GenericVar"/> or a
 /// <see cref="GenericMVar"/>.
 /// </summary>
 /// <param name="type">Type</param>
 /// <returns><c>true</c> if <paramref name="type"/> contains a <see cref="GenericVar"/> or a
 /// <see cref="GenericMVar"/>.</returns>
 public static bool ContainsGenericParameter(ExportedType type)
 {
     return(new TypeHelper().ContainsGenericParameterInternal(type));
 }
		/// <summary>
		/// Gets the scope type
		/// </summary>
		/// <param name="exportedType">The <c>ExportedType</c></param>
		/// <returns>The scope type or <c>null</c> if none found</returns>
		public static ITypeDefOrRef ScopeType(ExportedType exportedType) {
			return new FullNameCreator().GetScopeType(exportedType);
		}
Esempio n. 32
0
 bool ContainsGenericParameterInternal(ExportedType type)
 {
     return(false);
 }
		/// <summary>
		/// Returns the owner module. The type was created from metadata in this module.
		/// </summary>
		/// <param name="exportedType">The <c>ExportedType</c></param>
		/// <returns>A <see cref="ModuleDef"/> or <c>null</c> if none found</returns>
		public static ModuleDef OwnerModule(ExportedType exportedType) {
			return new FullNameCreator().GetOwnerModule(exportedType);
		}
Esempio n. 34
0
 /// <summary>
 /// Returns the namespace of a <see cref="ExportedType"/>
 /// </summary>
 /// <param name="exportedType">The <c>ExportedType</c></param>
 /// <param name="isReflection">Set if output should be compatible with reflection</param>
 /// <returns>The namespace</returns>
 public static string Namespace(ExportedType exportedType, bool isReflection)
 {
     return NamespaceSB(exportedType, isReflection, null).ToString();
 }