本文整理汇总了C#中Internal.TypeSystem.MethodSignature类的典型用法代码示例。如果您正苦于以下问题:C# MethodSignature类的具体用法?C# MethodSignature怎么用?C# MethodSignature使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MethodSignature类属于Internal.TypeSystem命名空间,在下文中一共展示了MethodSignature类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitIL
public static MethodIL EmitIL(MethodDesc target)
{
Debug.Assert(target.Name == "Call");
Debug.Assert(target.Signature.Length > 0
&& target.Signature[0] == target.Context.GetWellKnownType(WellKnownType.IntPtr));
ILEmitter emitter = new ILEmitter();
var codeStream = emitter.NewCodeStream();
// Load all the arguments except the first one (IntPtr address)
for (int i = 1; i < target.Signature.Length; i++)
{
codeStream.EmitLdArg(i);
}
// now load IntPtr address
codeStream.EmitLdArg(0);
// Create a signature for the calli by copying the signature of the containing method
// while skipping the first argument
MethodSignature template = target.Signature;
TypeDesc returnType = template.ReturnType;
TypeDesc[] parameters = new TypeDesc[template.Length - 1];
for (int i = 1; i < template.Length; i++)
{
parameters[i - 1] = template[i];
}
var signature = new MethodSignature(template.Flags, 0, returnType, parameters);
codeStream.Emit(ILOpcode.calli, emitter.NewToken(signature));
codeStream.Emit(ILOpcode.ret);
return emitter.Link();
}
示例2: TestMethodLookup
public void TestMethodLookup()
{
MetadataType t = _testModule.GetType("GenericTypes", "GenericClass`1").MakeInstantiatedType(_context.GetWellKnownType(WellKnownType.Int32));
MethodSignature sig = new MethodSignature(MethodSignatureFlags.None, 0, t.Instantiation[0], new TypeDesc[0] { });
MethodDesc fooMethod = t.GetMethod("Foo", sig);
Assert.NotNull(fooMethod);
}
示例3: InitializeSignature
private MethodSignature InitializeSignature()
{
var metadataReader = MetadataReader;
BlobReader signatureReader = metadataReader.GetBlobReader(metadataReader.GetMethodDefinition(_handle).Signature);
EcmaSignatureParser parser = new EcmaSignatureParser(Module, signatureReader);
var signature = parser.ParseMethodSignature();
return (_signature = signature);
}
示例4: GetKnownMethod
/// <summary>
/// Retrieves a method on <paramref name="type"/> that is well known to the compiler.
/// Throws an exception if the method doesn't exist.
/// </summary>
public static MethodDesc GetKnownMethod(this TypeDesc type, string name, MethodSignature signature)
{
MethodDesc method = type.GetMethod(name, signature);
if (method == null)
{
throw new InvalidOperationException(String.Format("Expected method '{0}' not found on type '{1}'", name, type));
}
return method;
}
示例5: EmitIL
public static MethodIL EmitIL(MethodDesc target)
{
Debug.Assert(target.Name == "Call");
Debug.Assert(target.Signature.Length > 0
&& target.Signature[0] == target.Context.GetWellKnownType(WellKnownType.IntPtr));
ILEmitter emitter = new ILEmitter();
var codeStream = emitter.NewCodeStream();
// Load all the arguments except the first one (IntPtr address)
for (int i = 1; i < target.Signature.Length; i++)
{
codeStream.EmitLdArg(i);
}
// now load IntPtr address
codeStream.EmitLdArg(0);
// Create a signature for the calli by copying the signature of the containing method
// while skipping the first argument
MethodSignature template = target.Signature;
TypeDesc returnType = template.ReturnType;
TypeDesc[] parameters = new TypeDesc[template.Length - 1];
for (int i = 1; i < template.Length; i++)
{
parameters[i - 1] = template[i];
}
var signature = new MethodSignature(template.Flags, 0, returnType, parameters);
bool useTransformedCalli = true;
if ((signature.Flags & MethodSignatureFlags.UnmanagedCallingConventionMask) != 0)
{
// Fat function pointer only ever exist for managed targets.
useTransformedCalli = false;
}
if (((MetadataType)target.OwningType).Name == "RawCalliHelper")
{
// RawCalliHelper doesn't need the transform.
useTransformedCalli = false;
}
if (useTransformedCalli)
EmitTransformedCalli(emitter, codeStream, signature);
else
codeStream.Emit(ILOpcode.calli, emitter.NewToken(signature));
codeStream.Emit(ILOpcode.ret);
return emitter.Link(target);
}
示例6: TestVirtualDispatchOnGenericType
public void TestVirtualDispatchOnGenericType()
{
// Verifies that virtual dispatch to a non-generic method on a generic instantiation works
DefType objectType = _context.GetWellKnownType(WellKnownType.Object);
MethodSignature toStringSig = new MethodSignature(MethodSignatureFlags.None, 0, _stringType, Array.Empty<TypeDesc>());
MethodDesc objectToString = objectType.GetMethod("ToString", toStringSig);
Assert.NotNull(objectToString);
MetadataType openTestType = _testModule.GetType("VirtualFunctionOverride", "SimpleGeneric`1");
InstantiatedType testInstance = openTestType.MakeInstantiatedType(new Instantiation(new TypeDesc[] { objectType }));
MethodDesc targetOnInstance = testInstance.GetMethod("ToString", toStringSig);
MethodDesc targetMethod = VirtualFunctionResolution.FindVirtualFunctionTargetMethodOnObjectType(objectToString, testInstance);
Assert.Equal(targetOnInstance, targetMethod);
}
示例7: GetStringInitializer
/// <summary>
/// NEWOBJ operation on String type is actually a call to a static method that returns a String
/// instance (i.e. there's an explicit call to the runtime allocator from the static method body).
/// This method returns the alloc+init helper corresponding to a given string constructor.
/// </summary>
public static MethodDesc GetStringInitializer(this MethodDesc constructorMethod)
{
Debug.Assert(constructorMethod.IsConstructor);
Debug.Assert(constructorMethod.OwningType.IsString);
var constructorSignature = constructorMethod.Signature;
// There's an extra (useless) Object as the first arg to match RyuJIT expectations.
var parameters = new TypeDesc[constructorSignature.Length + 1];
parameters[0] = constructorMethod.Context.GetWellKnownType(WellKnownType.Object);
for (int i = 0; i < constructorSignature.Length; i++)
parameters[i + 1] = constructorSignature[i];
MethodSignature sig = new MethodSignature(
MethodSignatureFlags.Static, 0, constructorMethod.OwningType, parameters);
MethodDesc result = constructorMethod.OwningType.GetKnownMethod("Ctor", sig);
return result;
}
示例8: EmitIL
public static MethodIL EmitIL(MethodDesc target)
{
Debug.Assert(target.Name == "CompareExchange" || target.Name == "Exchange");
//
// Find non-generic method to forward the generic method to.
//
int parameterCount = target.Signature.Length;
Debug.Assert(parameterCount == 3 || parameterCount == 2);
var objectType = target.Context.GetWellKnownType(WellKnownType.Object);
var parameters = new TypeDesc[parameterCount];
parameters[0] = objectType.MakeByRefType();
for (int i = 1; i < parameters.Length; i++)
parameters[i] = objectType;
MethodSignature nonGenericSignature = new MethodSignature(MethodSignatureFlags.Static, 0, objectType, parameters);
MethodDesc nonGenericMethod = target.OwningType.GetMethod(target.Name, nonGenericSignature);
// TODO: Better exception type. Should be: "CoreLib doesn't have a required thing in it".
if (nonGenericMethod == null)
throw new NotImplementedException();
//
// Emit the forwarder
//
ILEmitter emitter = new ILEmitter();
var codeStream = emitter.NewCodeStream();
// Reload all arguments
for (int i = 0; i < parameterCount; i++)
codeStream.EmitLdArg(i);
codeStream.Emit(ILOpcode.call, emitter.NewToken(nonGenericMethod));
codeStream.Emit(ILOpcode.ret);
return emitter.Link();
}
示例9: TestStandaloneSignatureGeneration
public void TestStandaloneSignatureGeneration()
{
var transformResult = MetadataTransform.Run(new SingleFileMetadataPolicy(), new[] { _systemModule });
var stringRecord = transformResult.GetTransformedTypeDefinition(
(Cts.MetadataType)_context.GetWellKnownType(Cts.WellKnownType.String));
var singleRecord = transformResult.GetTransformedTypeDefinition(
(Cts.MetadataType)_context.GetWellKnownType(Cts.WellKnownType.Single));
var sig = new Cts.MethodSignature(
0, 0, _context.GetWellKnownType(Cts.WellKnownType.String),
new[] { _context.GetWellKnownType(Cts.WellKnownType.Single) });
var sigRecord = transformResult.Transform.HandleMethodSignature(sig);
// Verify the signature is connected to the existing transformResult world
Assert.Same(stringRecord, sigRecord.ReturnType.Type);
Assert.Equal(1, sigRecord.Parameters.Count);
Assert.Same(singleRecord, sigRecord.Parameters[0].Type);
}
示例10: GetStringInitializer
/// <summary>
/// NEWOBJ operation on String type is actually a call to a static method that returs a String
/// instance (i.e. there's an explict call to the runtime allocator from the static method body).
/// This method returns the alloc+init helper corresponding to a given string constructor.
/// </summary>
public static MethodDesc GetStringInitializer(MethodDesc constructorMethod)
{
Debug.Assert(constructorMethod.IsConstructor);
Debug.Assert(constructorMethod.OwningType.IsString);
// There's an extra (useless) Object as the first arg to match RyuJIT expectations.
var parameters = new TypeDesc[constructorMethod.Signature.Length + 1];
parameters[0] = constructorMethod.Context.GetWellKnownType(WellKnownType.Object);
for (int i = 0; i < constructorMethod.Signature.Length; i++)
parameters[i + 1] = constructorMethod.Signature[i];
MethodSignature sig = new MethodSignature(
MethodSignatureFlags.Static, 0, constructorMethod.OwningType, parameters);
MethodDesc result = constructorMethod.OwningType.GetMethod("Ctor", sig);
// TODO: Better exception type. Should be: "CoreLib doesn't have a required thing in it".
if (result == null)
throw new NotImplementedException();
return result;
}
示例11: Equals
public bool Equals(MethodSignature otherSignature)
{
// TODO: Generics, etc.
if (this._flags != otherSignature._flags)
return false;
if (this._genericParameterCount != otherSignature._genericParameterCount)
return false;
if (this._returnType != otherSignature._returnType)
return false;
if (this._parameters.Length != otherSignature._parameters.Length)
return false;
for (int i = 0; i < this._parameters.Length; i++)
{
if (this._parameters[i] != otherSignature._parameters[i])
return false;
}
return true;
}
示例12: EmitIL
public static MethodIL EmitIL(MethodDesc method)
{
if (!RequiresMarshalling(method))
return null;
try
{
return new PInvokeMarshallingILEmitter(method).EmitIL();
}
catch (NotSupportedException)
{
ILEmitter emitter = new ILEmitter();
string message = "Method '" + method.ToString() +
"' requires non-trivial marshalling that is not yet supported by this compiler.";
TypeSystemContext context = method.Context;
MethodSignature ctorSignature = new MethodSignature(0, 0, context.GetWellKnownType(WellKnownType.Void),
new TypeDesc[] { context.GetWellKnownType(WellKnownType.String) });
MethodDesc exceptionCtor = method.Context.GetWellKnownType(WellKnownType.Exception).GetKnownMethod(".ctor", ctorSignature);
ILCodeStream codeStream = emitter.NewCodeStream();
codeStream.Emit(ILOpcode.ldstr, emitter.NewToken(message));
codeStream.Emit(ILOpcode.newobj, emitter.NewToken(exceptionCtor));
codeStream.Emit(ILOpcode.throw_);
codeStream.Emit(ILOpcode.ret);
return emitter.Link();
}
}
示例13: AppendCppMethodDeclaration
public void AppendCppMethodDeclaration(CppGenerationBuffer sb, MethodDesc method, bool implementation, string externalMethodName = null, MethodSignature methodSignature = null)
{
if (methodSignature == null)
methodSignature = method.Signature;
if (externalMethodName != null)
{
sb.Append("extern \"C\" ");
}
else
{
if (!implementation)
{
sb.Append("static ");
}
}
sb.Append(GetCppSignatureTypeName(methodSignature.ReturnType));
sb.Append(" ");
if (externalMethodName != null)
{
sb.Append(externalMethodName);
}
else
{
if (implementation)
{
sb.Append(GetCppMethodDeclarationName(method.OwningType, GetCppMethodName(method)));
}
else
{
sb.Append(GetCppMethodName(method));
}
}
sb.Append("(");
bool hasThis = !methodSignature.IsStatic;
int argCount = methodSignature.Length;
if (hasThis)
argCount++;
List<string> parameterNames = null;
if (method != null)
{
IEnumerable<string> parameters = GetParameterNamesForMethod(method);
if (parameters != null)
{
parameterNames = new List<string>(parameters);
if (parameterNames.Count != 0)
{
System.Diagnostics.Debug.Assert(parameterNames.Count == argCount);
}
else
{
parameterNames = null;
}
}
}
for (int i = 0; i < argCount; i++)
{
if (hasThis)
{
if (i == 0)
{
var thisType = method.OwningType;
if (thisType.IsValueType)
thisType = thisType.MakeByRefType();
sb.Append(GetCppSignatureTypeName(thisType));
}
else
{
sb.Append(GetCppSignatureTypeName(methodSignature[i - 1]));
}
}
else
{
sb.Append(GetCppSignatureTypeName(methodSignature[i]));
}
if (implementation)
{
sb.Append(" ");
if (parameterNames != null)
{
sb.Append(SanitizeCppVarName(parameterNames[i]));
}
else
{
sb.Append("_a");
sb.Append(i.ToStringInvariant());
}
}
if (i != argCount - 1)
sb.Append(", ");
}
sb.Append(")");
if (!implementation)
sb.Append(";");
}
示例14: GetMethod
/// <summary>
/// Gets a named method on the type. This method only looks at methods defined
/// in type's metadata. The <paramref name="signature"/> parameter can be null.
/// If signature is not specified and there are multiple matches, the first one
/// is returned. Returns null if method not found.
/// </summary>
// TODO: Substitutions, generics, modopts, ...
public virtual MethodDesc GetMethod(string name, MethodSignature signature)
{
foreach (var method in GetMethods())
{
if (method.Name == name)
{
if (signature == null || signature.Equals(method.Signature))
return method;
}
}
return null;
}
示例15: MethodSignatureBuilder
public MethodSignatureBuilder(MethodSignature template)
{
_template = template;
_flags = template._flags;
_genericParameterCount = template._genericParameterCount;
_returnType = template._returnType;
_parameters = template._parameters;
}