Ejemplo n.º 1
0
        IMemberRefParent Import(IMemberRefParent parent)
        {
            var tdr = parent as ITypeDefOrRef;

            if (tdr != null)
            {
                var td = tdr as TypeDef;
                if (td != null && td.IsGlobalModuleType)
                {
                    var om = td.Module;
                    return(module.UpdateRowId(new ModuleRefUser(module, om == null ? null : om.Name)));
                }
                return(Import(tdr));
            }

            var modRef = parent as ModuleRef;

            if (modRef != null)
            {
                return(module.UpdateRowId(new ModuleRefUser(module, modRef.Name)));
            }

            var method = parent as MethodDef;

            if (method != null)
            {
                var dt = method.DeclaringType;
                return(dt == null || dt.Module != module ? null : method);
            }

            return(null);
        }
Ejemplo n.º 2
0
 public MemberRefOptions(MemberRef mr)
 {
     this.Class = mr.Class;
     this.Name = mr.Name;
     this.Signature = mr.Signature;
     this.CustomAttributes.AddRange(mr.CustomAttributes);
 }
Ejemplo n.º 3
0
 public MemberRefOptions(MemberRef mr)
 {
     this.Class     = mr.Class;
     this.Name      = mr.Name;
     this.Signature = mr.Signature;
     this.CustomAttributes.AddRange(mr.CustomAttributes);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a new reference to a member in an (external) .NET assembly.
 /// </summary>
 /// <param name="parent">The declaring member that defines the referenced member.</param>
 /// <param name="name">The name of the referenced member.</param>
 /// <param name="signature">The signature of the referenced member. This dictates whether the
 /// referenced member is a field or a method.</param>
 public MemberReference(IMemberRefParent parent, string name, MemberSignature signature)
     : this(new MetadataToken(TableIndex.MemberRef, 0))
 {
     Parent    = parent;
     Name      = name;
     Signature = signature;
 }
Ejemplo n.º 5
0
        public MetadataToken GetMemberRefParentToken(IMemberRefParent parent)
        {
            AssertIsImported(parent);

            var type = parent as ITypeDefOrRef;

            if (type != null)
            {
                return(GetTypeToken(type));
            }

            var method = parent as MethodDefinition;

            if (method != null)
            {
                return(GetNewToken(method));
            }

            var reference = parent as ModuleReference;

            if (reference != null)
            {
                return(GetModuleReferenceToken(reference));
            }

            throw new NotSupportedException("Invalid or unsupported MemberRefParent reference " + parent + ".");
        }
Ejemplo n.º 6
0
 public MemberReference(IMemberRefParent parent, string name, MemberSignature signature)
     : base(null, new MetadataToken(MetadataTokenType.MemberRef), new MetadataRow <uint, uint, uint>())
 {
     _parent    = new LazyValue <IMemberRefParent>(parent);
     _name      = new LazyValue <string>(name);
     _signature = new LazyValue <MemberSignature>(signature);
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Determines whether two member parents are considered equal according to their signature.
        /// </summary>
        /// <param name="parent1">The first member parent to compare.</param>
        /// <param name="parent2">The second member parent to compare.</param>
        /// <returns><c>True</c> if the parents are considered equal, <c>False</c> otherwise.</returns>
        public bool Equals(IMemberRefParent parent1, IMemberRefParent parent2)
        {
            if (parent1 == null && parent2 == null)
            {
                return(true);
            }
            if (parent1 == null || parent2 == null)
            {
                return(false);
            }

            var type = parent1 as ITypeDefOrRef;

            if (type != null)
            {
                return(Equals(type, parent2 as ITypeDefOrRef));
            }

            var moduleRef = parent1 as ModuleReference;

            if (moduleRef != null)
            {
                return(Equals(moduleRef, parent2 as ModuleReference));
            }

            return(false);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="module">Owner module</param>
 /// <param name="name">Name of method ref</param>
 /// <param name="sig">Method sig</param>
 /// <param name="class">Owner of method</param>
 public MemberRefUser(ModuleDef module, UTF8String name, MethodSig sig, IMemberRefParent @class)
 {
     this.module    = module;
     this.name      = name;
     this.@class    = @class;
     this.signature = sig;
 }
Ejemplo n.º 9
0
 public MemberReference(IMemberRefParent parent, string name, MemberSignature signature)
     : base(new MetadataToken(MetadataTokenType.MemberRef))
 {
     _parent          = new LazyValue <IMemberRefParent>(parent);
     _name            = new LazyValue <string>(name);
     _signature       = new LazyValue <MemberSignature>(signature);
     CustomAttributes = new CustomAttributeCollection(this);
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Imports a parent of a member reference into the assembly.
        /// </summary>
        /// <param name="parent">The parent to import.</param>
        /// <returns>The imported parent.</returns>
        public IMemberRefParent ImportMemberRefParent(IMemberRefParent parent)
        {
            switch (parent)
            {
            case ITypeDefOrRef type:
                return(ImportType(type));

            case ModuleReference moduleRef:
                return(ImportModule(moduleRef));

            default:
                throw new NotSupportedException();
            }
        }
Ejemplo n.º 11
0
        public MemberRef Method(bool isInstance, string name, IMemberRefParent declaringType, TypeSig returnType, params TypeSig[] args)
        {
            MethodSig sig;

            if (isInstance)
            {
                sig = MethodSig.CreateInstance(returnType, args);
            }
            else
            {
                sig = MethodSig.CreateStatic(returnType, args);
            }
            return(module.UpdateRowId(new MemberRefUser(module, name, sig, declaringType)));
        }
Ejemplo n.º 12
0
 static bool Equals(IMemberRefParent @class, TypeDef type)
 {
     if (@class == null || type == null)
     {
         return(false);
     }
     if (@class is TypeDef td)
     {
         return(TypeEqualityComparerInstance.Equals(td, type));
     }
     if (@class is TypeRef tr)
     {
         return(TypeEqualityComparerInstance.Equals(tr, type));
     }
     if (@class is TypeSpec ts)
     {
         var typeSig = ts.TypeSig.RemovePinnedAndModifiers();
         var tdrSig  = typeSig as TypeDefOrRefSig;
         if (tdrSig == null && typeSig is GenericInstSig gis)
         {
             tdrSig = gis.GenericType;
         }
         if (tdrSig != null)
         {
             if (tdrSig.TypeDefOrRef is TypeDef td2)
             {
                 return(TypeEqualityComparerInstance.Equals(td2, type));
             }
             if (tdrSig.TypeDefOrRef is TypeRef tr2)
             {
                 return(TypeEqualityComparerInstance.Equals(tr2, type));
             }
             return(false);
         }
         return(false);
     }
     if (@class is MethodDef md)
     {
         return(TypeEqualityComparerInstance.Equals(md.DeclaringType, type));
     }
     if (@class is ModuleRef mr)
     {
         return(type.IsGlobalModuleType && StringComparer.OrdinalIgnoreCase.Equals(mr.Name, type.Module.Name));
     }
     return(false);
 }
Ejemplo n.º 13
0
        public IMemberRefParent ImportMemberRefParent(IMemberRefParent parent)
        {
            var type = parent as ITypeDefOrRef;

            if (type != null)
            {
                return(ImportType(type));
            }

            var moduleRef = parent as ModuleReference;

            if (moduleRef != null)
            {
                return(ImportModule(moduleRef));
            }

            throw new NotSupportedException();
        }
Ejemplo n.º 14
0
        private uint AddMemberRefParent(IMemberRefParent parent)
        {
            if (parent is null || !AssertIsImported(parent))
            {
                return(0);
            }

            var token = parent.MetadataToken.Table switch
            {
                TableIndex.TypeDef => GetTypeDefinitionToken(parent as TypeDefinition),
                TableIndex.TypeRef => GetTypeReferenceToken(parent as TypeReference),
                TableIndex.TypeSpec => GetTypeSpecificationToken(parent as TypeSpecification),
                TableIndex.Method => GetMethodDefinitionToken(parent as MethodDefinition),
                TableIndex.ModuleRef => GetModuleReferenceToken(parent as ModuleReference),
                _ => throw new ArgumentOutOfRangeException(nameof(parent))
            };

            return(Metadata.TablesStream
                   .GetIndexEncoder(CodedIndex.MemberRefParent)
                   .EncodeToken(token));
        }
Ejemplo n.º 15
0
 string GetDeclaringTypeFullName(IMemberRefParent parent)
 {
     if (parent == null)
     {
         return(null);
     }
     if (parent is ITypeDefOrRef)
     {
         return(((ITypeDefOrRef)parent).FullName);
     }
     if (parent is ModuleRef)
     {
         return(string.Format("[module:{0}]<Module>", ((ModuleRef)parent).ToString()));
     }
     if (parent is MethodDef)
     {
         var declaringType = ((MethodDef)parent).DeclaringType;
         return(declaringType == null ? null : declaringType.FullName);
     }
     return(null);               // Should never be reached
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Determines whether two member parents are considered equal according to their signature.
        /// </summary>
        /// <param name="parent1">The first member parent to compare.</param>
        /// <param name="parent2">The second member parent to compare.</param>
        /// <returns><c>True</c> if the parents are considered equal, <c>False</c> otherwise.</returns>
        public bool Equals(IMemberRefParent parent1, IMemberRefParent parent2)
        {
            if (parent1 == null && parent2 == null)
            {
                return(true);
            }
            if (parent1 == null || parent2 == null)
            {
                return(false);
            }

            switch (parent1)
            {
            case ITypeDefOrRef type:
                return(Equals(type, parent2 as ITypeDefOrRef));

            case ModuleReference moduleRef:
                return(Equals(moduleRef, parent2 as ModuleReference));

            default:
                return(false);
            }
        }
Ejemplo n.º 17
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="module">Owner module</param>
		/// <param name="name">Name of method ref</param>
		/// <param name="sig">Method sig</param>
		/// <param name="class">Owner of method</param>
		public MemberRefUser(ModuleDef module, UTF8String name, MethodSig sig, IMemberRefParent @class) {
			this.module = module;
			this.name = name;
			this.@class = @class;
			this.signature = sig;
		}
Ejemplo n.º 18
0
		string GetDeclaringTypeFullName(IMemberRefParent parent) {
			if (parent == null)
				return null;
			if (parent is ITypeDefOrRef)
				return ((ITypeDefOrRef)parent).FullName;
			if (parent is ModuleRef)
				return string.Format("[module:{0}]<Module>", ((ModuleRef)parent).ToString());
			if (parent is MethodDef) {
				var declaringType = ((MethodDef)parent).DeclaringType;
				return declaringType == null ? null : declaringType.FullName;
			}
			return null;	// Should never be reached
		}
Ejemplo n.º 19
0
		static bool IsFromNonObfuscatedAssembly(IMemberRefParent mrp) {
			return IsFromNonObfuscatedAssembly(mrp as TypeRef);
		}
Ejemplo n.º 20
0
        public IMemberRefParent ImportMemberRefParent(IMemberRefParent parent)
        {
            var type = parent as ITypeDefOrRef;
            if (type != null)
                return ImportType(type);

            var moduleRef = parent as ModuleReference;
            if (moduleRef != null)
                return ImportModule(moduleRef);

            throw new NotSupportedException();
        }
Ejemplo n.º 21
0
		TypeDef GetDeclaringType(MemberRef memberRef, IMemberRefParent parent) {
			if (memberRef == null || parent == null)
				return null;

			var declaringTypeDef = parent as TypeDef;
			if (declaringTypeDef != null)
				return declaringTypeDef;

			var declaringTypeRef = parent as TypeRef;
			if (declaringTypeRef != null)
				return Resolve(declaringTypeRef);

			// A module ref is used to reference the global type of a module in the same
			// assembly as the current module.
			var moduleRef = parent as ModuleRef;
			if (moduleRef != null) {
				var module = memberRef.Module;
				if (module == null)
					return null;
				TypeDef globalType = null;
				if (new SigComparer(0).Equals(module, moduleRef))
					globalType = module.GlobalType;
				var modAsm = module.Assembly;
				if (globalType == null && modAsm != null) {
					var moduleDef = modAsm.FindModule(moduleRef.Name);
					if (moduleDef != null)
						globalType = moduleDef.GlobalType;
				}
				return globalType;
			}

			var method = parent as MethodDef;
			if (method != null)
				return method.DeclaringType;

			var ts = parent as TypeSpec;
			if (ts != null) {
				var git = ts.TypeSig as GenericInstSig;
				if (git != null) {
					var td = git.GenericType.TypeDef;
					if (td != null)
						return td;
					var tr = git.GenericType.TypeRef;
					if (tr != null)
						return Resolve(tr);
				}
				return null;
			}

			return null;
		}
Ejemplo n.º 22
0
        /// <summary>
        /// Gets a <see cref="GenericParamContext"/> that can be used as signature context
        /// </summary>
        /// <param name="gpContext">Context passed to the constructor</param>
        /// <param name="class">Field/method class owner</param>
        /// <returns></returns>
        protected static GenericParamContext GetSignatureGenericParamContext(GenericParamContext gpContext, IMemberRefParent @class)
        {
            TypeDef type = null;
            MethodDef method = gpContext.Method;

            var ts = @class as TypeSpec;
            if (ts != null) {
                var gis = ts.TypeSig as GenericInstSig;
                if (gis != null)
                    type = gis.GenericType.ToTypeDefOrRef().ResolveTypeDef();
            }

            return new GenericParamContext(type, method);
        }
Ejemplo n.º 23
0
 static bool IsFromNonObfuscatedAssembly(IMemberRefParent mrp)
 {
     return(IsFromNonObfuscatedAssembly(mrp as TypeRef));
 }
		public MemberRef Method(bool isInstance, string name, IMemberRefParent declaringType, TypeSig returnType, params TypeSig[] args) {
			MethodSig sig;
			if (isInstance)
				sig = MethodSig.CreateInstance(returnType, args);
			else
				sig = MethodSig.CreateStatic(returnType, args);
			return module.UpdateRowId(new MemberRefUser(module, name, sig, declaringType));
		}
Ejemplo n.º 25
0
 public MemberRef InstanceMethod(string name, IMemberRefParent declaringType, TypeSig returnType, params TypeSig[] args)
 {
     return(Method(true, name, declaringType, returnType, args));
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Gets a <see cref="GenericParamContext"/> that can be used as signature context
        /// </summary>
        /// <param name="gpContext">Context passed to the constructor</param>
        /// <param name="class">Field/method class owner</param>
        /// <returns></returns>
        protected static GenericParamContext GetSignatureGenericParamContext(GenericParamContext gpContext, IMemberRefParent @class)
        {
            TypeDef   type   = null;
            MethodDef method = gpContext.Method;

            var ts = @class as TypeSpec;

            if (ts != null)
            {
                var gis = ts.TypeSig as GenericInstSig;
                if (gis != null)
                {
                    type = gis.GenericType.ToTypeDefOrRef().ResolveTypeDef();
                }
            }

            return(new GenericParamContext(type, method));
        }
Ejemplo n.º 27
0
        TypeDef GetDeclaringType(MemberRef memberRef, IMemberRefParent parent)
        {
            if (memberRef == null || parent == null)
            {
                return(null);
            }

            var ts = parent as TypeSpec;

            if (ts != null)
            {
                parent = ts.ScopeType;
            }

            var declaringTypeDef = parent as TypeDef;

            if (declaringTypeDef != null)
            {
                return(declaringTypeDef);
            }

            var declaringTypeRef = parent as TypeRef;

            if (declaringTypeRef != null)
            {
                return(Resolve(declaringTypeRef, memberRef.Module));
            }

            // A module ref is used to reference the global type of a module in the same
            // assembly as the current module.
            var moduleRef = parent as ModuleRef;

            if (moduleRef != null)
            {
                var module = memberRef.Module;
                if (module == null)
                {
                    return(null);
                }
                TypeDef globalType = null;
                if (new SigComparer().Equals(module, moduleRef))
                {
                    globalType = module.GlobalType;
                }
                var modAsm = module.Assembly;
                if (globalType == null && modAsm != null)
                {
                    var moduleDef = modAsm.FindModule(moduleRef.Name);
                    if (moduleDef != null)
                    {
                        globalType = moduleDef.GlobalType;
                    }
                }
                return(globalType);
            }

            var method = parent as MethodDef;

            if (method != null)
            {
                return(method.DeclaringType);
            }

            return(null);
        }
		public MemberRef InstanceMethod(string name, IMemberRefParent declaringType, TypeSig returnType, params TypeSig[] args) {
			return Method(true, name, declaringType, returnType, args);
		}
Ejemplo n.º 29
0
 public MemberRef StaticMethod(string name, IMemberRefParent declaringType, TypeSig returnType, params TypeSig[] args)
 {
     return(Method(false, name, declaringType, returnType, args));
 }
		public MemberRef StaticMethod(string name, IMemberRefParent declaringType, TypeSig returnType, params TypeSig[] args) {
			return Method(false, name, declaringType, returnType, args);
		}
Ejemplo n.º 31
0
        /// <summary>
        /// Determines whether two member parents are considered equal according to their signature.
        /// </summary>
        /// <param name="parent1">The first member parent to compare.</param>
        /// <param name="parent2">The second member parent to compare.</param>
        /// <returns><c>True</c> if the parents are considered equal, <c>False</c> otherwise.</returns>
        public bool MatchParents(IMemberRefParent parent1, IMemberRefParent parent2)
        {
            if (parent1 == null && parent2 == null)
                return true;
            if (parent1 == null || parent2 == null)
                return false;

            var type = parent1 as ITypeDefOrRef;
            if (type != null)
                return MatchTypes(type, parent2 as ITypeDefOrRef);

            var moduleRef = parent1 as ModuleReference;
            if (moduleRef != null)
                return MatchModules(moduleRef, parent2 as ModuleReference);

            return false;
        }