本文整理汇总了C#中Mono.Cecil.MethodReference.GetElementMethod方法的典型用法代码示例。如果您正苦于以下问题:C# MethodReference.GetElementMethod方法的具体用法?C# MethodReference.GetElementMethod怎么用?C# MethodReference.GetElementMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.MethodReference
的用法示例。
在下文中一共展示了MethodReference.GetElementMethod方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Resolve
public static MethodDefinition Resolve(IAssemblyResolver resolver, MethodReference method)
{
var type = Resolve (resolver, method.DeclaringType);
if (type == null)
return null;
method = method.GetElementMethod ();
if (!type.HasMethods)
return null;
return GetMethod (resolver, type, method);
}
示例2: ResolveManually
private MethodDefinition ResolveManually(MethodReference method)
{
var metadataResolver = method.Module.MetadataResolver;
var type = metadataResolver.Resolve(method.DeclaringType);
if (type == null || !type.HasMethods)
{
return null;
}
method = method.GetElementMethod();
Func<IEnumerable<MethodDefinition>, MethodReference, MethodDefinition>[] finderMethods =
{GetMethodDefinition, GetCompatibleMethodDefinition};
for (int i = 0; i < finderMethods.Length; i++)
{
while (type != null)
{
var methodDefinition = finderMethods[i](type.Methods, method);
if (methodDefinition != null)
{
return methodDefinition;
}
if (type.BaseType == null)
{
break;
}
type = metadataResolver.Resolve(type.BaseType);
}
type = metadataResolver.Resolve(method.DeclaringType);
}
return null;
}
示例3: TryToResolveInSupport
private bool TryToResolveInSupport(MethodReference method)
{
if (string.IsNullOrEmpty(Context.SupportModulePartialNamespace))
return false;
method = method.GetElementMethod();
var originalType = method.DeclaringType;
if (originalType.IsGenericInstance || originalType.HasGenericParameters)
return false;
var support = SupportAssemblyReference();
var ns = Context.SupportModulePartialNamespace;
if (!string.IsNullOrEmpty(originalType.Namespace))
ns += '.' + originalType.Namespace;
method.DeclaringType = new TypeReference(ns, originalType.Name, Context.TargetModule, support, originalType.IsValueType);
MethodDefinition resolved = null;
// We can only change declaring type like this for static methods
if (!method.HasThis)
{
resolved = method.Resolve();
}
// If our method is instance, we can have a static method in support module that has explicit "this" parameter
if (resolved == null && method.HasThis)
{
method.HasThis = false;
method.Parameters.Insert(0, new ParameterDefinition(originalType));
resolved = method.Resolve();
// Our explicit "this" parameter can be of type System.Object
if (resolved == null)
{
method.Parameters[0] = new ParameterDefinition(method.DeclaringType.Module.TypeSystem.Object.Resolve());
resolved = method.Resolve();
}
if (resolved == null)
{
method.HasThis = true;
method.Parameters.RemoveAt(0);
}
}
if (resolved != null)
{
Context.RewriteTarget = true;
AddSupportReferenceIfNeeded(support);
return true;
}
method.DeclaringType = originalType;
return false;
}
示例4: Resolve
public virtual MethodDefinition Resolve (MethodReference method)
{
if (method == null)
throw new ArgumentNullException ("method");
var type = Resolve (method.DeclaringType);
if (type == null)
return null;
method = method.GetElementMethod ();
if (!type.HasMethods)
return null;
return GetMethod (type, method);
}
示例5: GetToken
private static ulong GetToken (MethodReference method)
{
return (ulong) method.DeclaringType.Module.Assembly.GetHashCode () << 32 | method.GetElementMethod ().MetadataToken.ToUInt32 ();
}
示例6: OnInvocation
//.........这里部分代码省略.........
if (mr.ReturnType.FullName == replacementMethod_.ReturnType.Resolve ().FullName) {
noCast = true;
if (Verbosity >= 9)
Console.WriteLine (" - (alt) generic method and alt method return same type:: " + mr.ReturnType.FullName);
} else if (mr.ReturnType.FullName != "System.Void") { // Replacement must return object
if (replacementMethod_.ReturnType.Resolve ().FullName != "System.Object") {
if (Verbosity >= 3)
Console.WriteLine (" [x? return] (alt) generic method returns T but candidate replacement method does not return System.Object: " + replacementMethod_.FullName
+ " [[" + mr.FullName + " in " + method.DeclaringType.FullName + "." + method.Name + "]]");
continue;
}
}
if (Verbosity >= 8)
Console.WriteLine ("FOUND ALTERNATE METHOD: " + replacementMethod_);
replacementMethod = replacementMethod_;
break; // FUTURE: don't break here, keep going to see if there are multiple (ambiguous) matches and throw/output error
}
if (replacementMethod == null) {
if (!(" " + mr.FullName).Contains (" System.")) {
if (Verbosity >= Verbosities.Warning)
Console.WriteLine ("[__] No alternate found for [["
+ mr.FullName + " in " + method.DeclaringType.FullName + "." + method.Name + "]]");
// + mr.DeclaringType.FullName+"." +mr.Name + "(...)");
}
skipped++;
return InvocationResult.Failed;
}
// if(mr.Name != "TestMethod") continue; // TEMP
if (Verbosity >= Verbosities.Success)
Console.WriteLine (" O Replacing " + mr.FullName
+ " " + mr.GenericParameters.Count + " generic parameters"
+ " " + mr.Parameters.Count + " parameters"
+ " | " + mr.GetElementMethod ().FullName + ""
+ " | " + mr.GetElementMethod ().HasGenericParameters + ""
+ " | " + mr.GetElementMethod ().GenericParameters [0].Name + ""
);
// if(Verbosity >= 6)
// Console.WriteLine("Resolved non-specific generic method: " + mr.FullName);
// if(Verbosity >= 8) Console.WriteLine("RESOLVED TYPE: " + genericTypeParameter);
// var typeModuleDefinition = ModuleDefinition.ReadModule(type.Module.Assembly.Location);
// var typeDefinition = typeModuleDefinition.Types.Where(td => td.FullName == genericParameter).FirstOrDefault();
// if(typeDefinition != null && Verbosity >= 5)
// {
// Console.WriteLine("Resolved typeDefinition: " + typeDefinition);
// }
// else
// {
// Console.WriteLine("Failed to resolve typeDefinition: " + type.FullName);
//// foreach(var td in ModuleDefinition.ReadModule(type.Module.Assembly.Location).Types)
//// {
//// Console.WriteLine(" ... " + td.FullName);
//// }
// continue;
// }
// method.Module.Import(type); // try removing this
// IMetadataScope scope = method.Module;
// var typeRef = new TypeReference(type.Namespace, type.Name, typeModuleDefinition, scope, type.IsValueType);
// Console.WriteLine("TypeRef: "+ typeRef);
// method.Module.Import(type);
示例7: Check
/// <summary>
/// Check the given method reference.
/// </summary>
private void Check(MethodReference method, string context)
{
if (!Check(method.DeclaringType, context))
return;
var error = false;
try
{
if (method.IsGenericInstance)
{
Check((GenericInstanceMethod)method, context);
}
else
{
var typeDef = method.DeclaringType.GetElementType().Resolve();
var emethod = method.GetElementMethod();
var methodDef = (typeDef != null) ? typeDef.Methods.FirstOrDefault(x => x.AreSameExcludingGenericArguments(emethod, null)) : null;
error = (methodDef == null);
}
}
catch (Exception)
{
error = true;
}
CheckResolveData data;
var key = method.FullName;
if (!resolveErrorMethodNames.TryGetValue(key, out data))
{
data = new CheckResolveData { IsAvailable = !error };
resolveErrorMethodNames.Add(key, data);
if (error)
{
Error(MessageTypes.MissingMethod, Format(method, false), method.DeclaringType.Scope, context);
}
}
data.CheckCount++;
}
示例8: GetToken
private static uint GetToken (MethodReference method)
{
return method.GetElementMethod ().MetadataToken.ToUInt32 ();
}
示例9: Resolve
public MethodDefinition Resolve (MethodReference method)
{
TypeDefinition type = Resolve (method.DeclaringType);
method = method.GetElementMethod ();
return GetMethod (type, method);
}
示例10: CreateFunction
/// <summary>
/// Creates the function.
/// </summary>
/// <param name="method">The method.</param>
/// <returns></returns>
Function CreateFunction(MethodReference method)
{
Function function;
if (functions.TryGetValue(method, out function))
return function;
var resolvedMethod = method.Resolve();
var declaringType = GetType(ResolveGenericsVisitor.Process(method, method.DeclaringType), TypeState.Opaque);
// Check if method is only defined in a parent class (can happen in some rare case, i.e. PCL TypeInfo.get_Assembly()).
bool hasMatch = MetadataResolver.GetMethod(declaringType.TypeDefinitionCecil.Methods, method.GetElementMethod()) != null;
if (resolvedMethod != null && !hasMatch)
{
var parentType = declaringType.TypeDefinitionCecil.BaseType != null ? ResolveGenericsVisitor.Process(declaringType.TypeReferenceCecil, declaringType.TypeDefinitionCecil.BaseType) : null;
if (parentType == null)
throw new InvalidOperationException(string.Format("Could not find a matching method in any of the type or its parent for {0}", method));
// Create function with parent type
// TODO: Maybe we need to replace generic context with parent type?
var parentMethod = method.ChangeDeclaringType(parentType);
function = CreateFunction(parentMethod);
// Register it so that it can be cached
functions.Add(method, function);
return function;
}
var returnType = GetType(ResolveGenericsVisitor.Process(method, method.ReturnType), TypeState.StackComplete);
var parameterTypesBuilder = new List<Type>();
if (method.HasThis)
{
var parameterType = declaringType.TypeReferenceCecil;
// Value type uses ByReference type for this
if (declaringType.TypeDefinitionCecil.IsValueType)
parameterType = parameterType.MakeByReferenceType();
parameterTypesBuilder.Add(GetType(parameterType, TypeState.StackComplete));
}
foreach (var parameter in method.Parameters)
{
parameterTypesBuilder.Add(GetType(ResolveGenericsVisitor.Process(method, parameter.ParameterType), TypeState.StackComplete));
}
var parameterTypes = parameterTypesBuilder.ToArray();
// Find calling convention
var callingConvention = method.CallingConvention;
PInvokeInfo pinvokeInfo = null;
if (resolvedMethod != null && resolvedMethod.HasPInvokeInfo)
{
pinvokeInfo = resolvedMethod.PInvokeInfo;
if (resolvedMethod.PInvokeInfo.IsCallConvStdCall || resolvedMethod.PInvokeInfo.IsCallConvWinapi)
callingConvention = MethodCallingConvention.StdCall;
else if (resolvedMethod.PInvokeInfo.IsCallConvFastcall)
callingConvention = MethodCallingConvention.FastCall;
}
var functionSignature = new FunctionSignature(abi, returnType, parameterTypes, callingConvention, pinvokeInfo);
var functionType = CreateFunctionTypeLLVM(functionSignature);
// If we have an external with generic parameters, let's try to do some generic sharing (we can write only one in C++)
bool isInternal = resolvedMethod != null && ((resolvedMethod.ImplAttributes & MethodImplAttributes.InternalCall) != 0);
if (isInternal && resolvedMethod.HasGenericParameters && resolvedMethod.GenericParameters.All(x => x.HasReferenceTypeConstraint))
{
// Check if this isn't the shareable method (in which case we should do normal processing)
if (!((GenericInstanceMethod)method).GenericArguments.All(x => MemberEqualityComparer.Default.Equals(x, @object.TypeReferenceCecil)))
{
// Let's share it with default method
var sharedGenericInstance = new GenericInstanceMethod(resolvedMethod);
foreach (var genericParameter in resolvedMethod.GenericParameters)
{
sharedGenericInstance.GenericArguments.Add(@object.TypeReferenceCecil);
}
var sharedMethod = GetFunction(sharedGenericInstance);
// Cast shared function to appropriate pointer type
var sharedFunctionGlobal = LLVM.ConstPointerCast(sharedMethod.GeneratedValue, LLVM.PointerType(functionType, 0));
function = new Function(declaringType, method, functionType, sharedFunctionGlobal, functionSignature);
functions.Add(method, function);
return function;
}
}
// Determine if type and function is local, and linkage type
bool isLocal;
var linkageType = GetLinkageType(method.DeclaringType, out isLocal);
if (isInternal)
{
// Should be switched to non-weak when we have complete implementation of every internal calls
linkageType = Linkage.ExternalWeakLinkage;
}
else if (resolvedMethod != null && resolvedMethod.HasGenericParameters)
//.........这里部分代码省略.........
示例11: WrapMethod
private StaticMethodWrapper WrapMethod(MethodReference methodRefHandle)
{
var declaringType = MakeDeclaredType(methodRefHandle.DeclaringType);
var declaringTypeDefnHandle = (TypeDefinition)declaringType.Handle;
var methodDefnHandle = FindMatchingMethod(methodRefHandle.GetElementMethod(), declaringTypeDefnHandle.Methods);
var method = new StaticMethodWrapper(this, methodDefnHandle, declaringType, declaringType, declaringType.Substitution);
var genericInstance = methodRefHandle as GenericInstanceMethod;
if (genericInstance != null)
{
var genericArguments = CollectionUtils.ConvertAllToArray<TypeReference, ITypeInfo>(genericInstance.GenericArguments, MakeType);
method = method.MakeGenericMethod(genericArguments);
}
return method;
}
示例12: WrapConstructor
private StaticConstructorWrapper WrapConstructor(MethodReference methodRefHandle)
{
var declaringType = MakeDeclaredType(methodRefHandle.DeclaringType);
var declaringTypeDefnHandle = (TypeDefinition)declaringType.Handle;
var methodDefnHandle = FindMatchingMethod(methodRefHandle.GetElementMethod(), GetConstructors(declaringTypeDefnHandle.Methods));
return new StaticConstructorWrapper(this, methodDefnHandle, declaringType);
}