本文整理汇总了C#中MethodBase.GetGenericArguments方法的典型用法代码示例。如果您正苦于以下问题:C# MethodBase.GetGenericArguments方法的具体用法?C# MethodBase.GetGenericArguments怎么用?C# MethodBase.GetGenericArguments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MethodBase
的用法示例。
在下文中一共展示了MethodBase.GetGenericArguments方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetFullNameForStackTrace
public static void GetFullNameForStackTrace (StringBuilder sb, MethodBase mi)
{
var declaringType = mi.DeclaringType;
if (declaringType.IsGenericType && !declaringType.IsGenericTypeDefinition)
declaringType = declaringType.GetGenericTypeDefinition ();
// Get generic definition
var bindingflags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
foreach (var m in declaringType.GetMethods (bindingflags)) {
if (m.MetadataToken == mi.MetadataToken) {
mi = m;
break;
}
}
sb.Append (declaringType.ToString ());
sb.Append (".");
sb.Append (mi.Name);
if (mi.IsGenericMethod) {
Type[] gen_params = mi.GetGenericArguments ();
sb.Append ("[");
for (int j = 0; j < gen_params.Length; j++) {
if (j > 0)
sb.Append (",");
sb.Append (gen_params [j].Name);
}
sb.Append ("]");
}
ParameterInfo[] p = mi.GetParameters ();
sb.Append (" (");
for (int i = 0; i < p.Length; ++i) {
if (i > 0)
sb.Append (", ");
Type pt = p[i].ParameterType;
if (pt.IsGenericType && ! pt.IsGenericTypeDefinition)
pt = pt.GetGenericTypeDefinition ();
sb.Append (pt.ToString());
if (p [i].Name != null) {
sb.Append (" ");
sb.Append (p [i].Name);
}
}
sb.Append (")");
}
示例2: GetGenericBaseType
static Type GetGenericBaseType(MethodBase md, Type t)
{
if (!md.IsGenericMethod)
{
return t;
}
List<Type> list = new List<Type>(md.GetGenericArguments());
if (list.Contains(t))
{
return t.BaseType;
}
return t;
}
示例3: CreateMethod
public MethodSpec CreateMethod (MethodBase mb, TypeSpec declaringType)
{
Modifiers mod = ReadMethodModifiers (mb, declaringType);
TypeParameterSpec[] tparams;
var parameters = CreateParameters (declaringType, mb.GetParameters (), mb);
if (mb.IsGenericMethod) {
if (!mb.IsGenericMethodDefinition)
throw new NotSupportedException ("assert");
tparams = CreateGenericParameters (0, mb.GetGenericArguments ());
} else {
tparams = null;
}
MemberKind kind;
TypeSpec returnType;
if (mb.MemberType == MemberTypes.Constructor) {
kind = MemberKind.Constructor;
returnType = module.Compiler.BuiltinTypes.Void;
} else {
//
// Detect operators and destructors
//
string name = mb.Name;
kind = MemberKind.Method;
if (tparams == null && !mb.DeclaringType.IsInterface && name.Length > 6) {
if ((mod & (Modifiers.STATIC | Modifiers.PUBLIC)) == (Modifiers.STATIC | Modifiers.PUBLIC)) {
if (name[2] == '_' && name[1] == 'p' && name[0] == 'o') {
var op_type = Operator.GetType (name);
if (op_type.HasValue && parameters.Count > 0 && parameters.Count < 3) {
kind = MemberKind.Operator;
}
}
} else if (parameters.IsEmpty && name == Destructor.MetadataName) {
kind = MemberKind.Destructor;
if (declaringType.BuiltinType == BuiltinTypeSpec.Type.Object) {
mod &= ~Modifiers.OVERRIDE;
mod |= Modifiers.VIRTUAL;
}
}
}
var mi = (MethodInfo) mb;
returnType = ImportType (mi.ReturnType, new DynamicTypeReader (mi.ReturnParameter));
// Cannot set to OVERRIDE without full hierarchy checks
// this flag indicates that the method could be override
// but further validation is needed
if ((mod & Modifiers.OVERRIDE) != 0) {
bool is_real_override = false;
if (kind == MemberKind.Method && declaringType.BaseType != null) {
var btype = declaringType.BaseType;
if (IsOverrideMethodBaseTypeAccessible (btype)) {
var filter = MemberFilter.Method (name, tparams != null ? tparams.Length : 0, parameters, null);
var candidate = MemberCache.FindMember (btype, filter, BindingRestriction.None);
//
// For imported class method do additional validation to be sure that metadata
// override flag was correct
//
// Difference between protected internal and protected is ok
//
const Modifiers conflict_mask = Modifiers.AccessibilityMask & ~Modifiers.INTERNAL;
if (candidate != null && (candidate.Modifiers & conflict_mask) == (mod & conflict_mask) && !candidate.IsStatic) {
is_real_override = true;
}
}
}
if (!is_real_override) {
mod &= ~Modifiers.OVERRIDE;
if ((mod & Modifiers.SEALED) != 0)
mod &= ~Modifiers.SEALED;
else
mod |= Modifiers.VIRTUAL;
}
}
}
IMemberDefinition definition;
if (tparams != null) {
var gmd = new ImportedGenericMethodDefinition ((MethodInfo) mb, returnType, parameters, tparams, this);
foreach (var tp in gmd.TypeParameters) {
ImportTypeParameterTypeConstraints (tp, tp.GetMetaInfo ());
}
definition = gmd;
} else {
definition = new ImportedParameterMemberDefinition (mb, returnType, parameters, this);
}
MethodSpec ms = new MethodSpec (kind, declaringType, definition, returnType, mb, parameters, mod);
if (tparams != null)
ms.IsGeneric = true;
return ms;
}
示例4: PrintMethodHead
string PrintMethodHead(MethodBase mb)
{
var s = new StringBuilder();
if (typeof(MethodInfo).IsInstanceOfType(mb))
{
s.Append(PrintTypeWithAssem(((MethodInfo)mb).ReturnType) + " ");
}
s.Append(mb.Name);
if (mb.IsGenericMethodDefinition)
{
Type[] ts = mb.GetGenericArguments(); // we must have at least one generic argument
s.Append(" <");
for (int i = 0; i < ts.Length; i++)
{
s.Append("("); // we should always have some generic constrains
Type[] cts = ts[i].GetGenericParameterConstraints();
Array.Sort(cts, sc);
for (int j = 0; j < cts.Length; j++)
{
s.Append(PrintTypeWithAssem(cts[j]));
if (j < cts.Length - 1)
s.Append(", ");
}
s.Append(") ");
s.Append(ts[i]);
if (i < (ts.Length - 1))
s.Append(", ");
}
s.Append(">");
}
else if (mb.IsGenericMethod)
{
s.Append(" <");
Type[] param = mb.GetGenericArguments();
for (int i = 0; i < param.Length; i++)
{
s.Append(PrintTypeWithAssem(param[i]));
if (i < (param.Length - 1))
s.Append(", ");
}
s.Append("> ");
}
s.Append(" (");
int count = 0;
foreach (ParameterInfo pa in mb.GetParameters())
{
// if the parameter is a generic parameter
// we excerise some more info that we can get
if (pa.ParameterType.IsGenericParameter)
{
if (pa.ParameterType.DeclaringMethod != null)
s.Append("!!" + pa.ParameterType.GenericParameterPosition + " " + pa.ParameterType);
else s.Append("!" + pa.ParameterType.GenericParameterPosition + " " + pa.ParameterType);
}
else
{
s.Append(PrintTypeWithAssem(pa.ParameterType));
}
if ((++count) < mb.GetParameters().Length)
s.Append(", ");
else s.Append(")");
}
if (count == 0) s.Append(")");
return s.ToString();
}
示例5: GetFullNameForStackTrace
internal void GetFullNameForStackTrace(StringBuilder sb, MethodBase mi)
{
ParameterInfo[] p = mi.GetParameters();
sb.Append(mi.DeclaringType.ToString());
sb.Append(".");
sb.Append(mi.Name);
if (mi.IsGenericMethod)
{
Type[] gen_params = mi.GetGenericArguments();
sb.Append("[");
for (int j = 0; j < gen_params.Length; j++)
{
if (j > 0)
sb.Append(",");
sb.Append(gen_params[j].Name);
}
sb.Append("]");
}
sb.Append(" (");
for (int i = 0; i < p.Length; ++i)
{
if (i > 0)
sb.Append(", ");
Type pt = p[i].ParameterType;
if (pt.IsClass && !String.IsNullOrEmpty(pt.Namespace))
{
sb.Append(pt.Namespace);
sb.Append(".");
}
sb.Append(pt.Name);
if (p[i].Name != null)
{
sb.Append(" ");
sb.Append(p[i].Name);
}
}
sb.Append(")");
}