/// <summary> /// Factory method for creating CRefPath entries when the type /// is unknown. /// </summary> /// <param name="member">The member to create a path for</param> /// <returns>The CRefPath</returns> /// <exception cref="NotImplementedException"> /// Thrown when the type of the <see cref="ReflectedMember"/> is not /// implemented. /// </exception> public static CRefPath Create(ReflectedMember member) { CRefPath path = null; if (member is FieldDef) { path = new CRefPath((FieldDef)member); } else if (member is TypeRef) { path = new CRefPath((TypeRef)member); } else if (member is PropertyDef) { path = new CRefPath((PropertyDef)member); } else if (member is EventDef) { path = new CRefPath((EventDef)member); } else if (member is MethodDef) { path = new CRefPath((MethodDef)member); } else { throw new NotImplementedException($"The '{member.GetType().ToString()}' type is not supported."); } return(path); }
/// <summary> /// Attempts to resolve the type. /// </summary> /// <param name="assembly"></param> /// <param name="member"></param> /// <returns></returns> internal TypeRef ResolveType(AssemblyDef assembly, ReflectedMember member) { TypeRef type = null; if (ElementType.ElementType == ElementTypes.SZArray) { TypeSignatureToken childType = (TypeSignatureToken)Tokens.Last(); type = childType.ResolveType(assembly, member); } else if ( ElementType.ElementType == ElementTypes.Class || ElementType.ElementType == ElementTypes.ValueType ) { type = (TypeRef)assembly.ResolveMetadataToken(ElementType.Token); } else if (ElementType.ElementType == ElementTypes.GenericInstance) { ElementTypeSignatureToken childType = (ElementTypeSignatureToken)Tokens[1]; type = childType.ResolveToken(assembly); } else if (ElementType.ElementType == ElementTypes.Ptr) { if (Tokens[1].TokenType == SignatureTokens.Type) { TypeSignatureToken childType = (TypeSignatureToken)Tokens[1]; type = childType.ResolveType(assembly, member); } else { ElementTypeSignatureToken childType = (ElementTypeSignatureToken)Tokens[1]; type = childType.ResolveToken(assembly); } } else if (ElementType.ElementType == ElementTypes.Array) { if (Tokens[1].TokenType == SignatureTokens.Type) { TypeSignatureToken childType = (TypeSignatureToken)Tokens[1]; type = childType.ResolveType(assembly, member); } else { ElementTypeSignatureToken childType = (ElementTypeSignatureToken)Tokens[1]; type = childType.ResolveToken(assembly); } } else if (ElementType.ElementType == ElementTypes.MVar) { MethodDef method = member as MethodDef; if (method == null) { InvalidOperationException ex = new InvalidOperationException( string.Format( "A MethodDef was expected for type resolution in the signiture but a {0} was provided.", member.GetType().ToString() )); throw ex; } type = method.GenericTypes[(int)ElementType.Token]; } else if (ElementType.ElementType == ElementTypes.Var) { // Anything that contains a Type property will do here TypeDef theType; if (member is MethodDef) { // a method may define its own parameters theType = (TypeDef)((MethodDef)member).Type; } else if (member is PropertyDef) { theType = ((PropertyDef)member).OwningType; } else if (member is FieldDef) { theType = (TypeDef)((FieldDef)member).Type; } else if (member is EventDef) { theType = ((EventDef)member).Type; } else if (member is TypeSpec) { theType = ((TypeSpec)member).ImplementingType; } else { InvalidOperationException ex = new InvalidOperationException( string.Format( "A ReflectedMember that is contained by a TypeDef was expected for type resolution in the signiture but a {0} was provided.", member.GetType().ToString() )); throw ex; } List <GenericTypeRef> genericParameters = theType.GenericTypes; if (genericParameters.Count < ElementType.Token) { throw new InvalidOperationException("The generic token refers to a parameter that is not available."); } type = genericParameters[(int)ElementType.Token]; } else if (ElementType.Definition != null) { type = (TypeRef)ElementType.Definition; } return(type); }