English 中文(简体)
签署
原标题:Get MethodInfo Signature

您的“方法”与我的方法有关:

void myMethod(int param1, int param2) { }

并且你想制造一种代表签名方法的插图:

string myString = "myMethod (int, int)";

通过方法学信息参数,我得以通过使用参数类型确定这些结果:

"myMethod (System.Int32, System.Int32)"

我如何能够改进这项工作并产生上述成果?

最佳回答
问题回答

As far as I know, there is nothing built-in that will convert the real type name of a primitive (System.Int32) to the built-in alias (int). Since there is only a very small number of these aliases, it s not too difficult to write your own method:

public static string GetTypeName(Type type) 
{
    if (type == typeof(int))  // Or "type == typeof(System.Int32)" -- same either way
        return "int";
    else if (type == typeof(long))
        return "long";
    ...
    else
        return type.Name;     // Or "type.FullName" -- not sure if you want the namespace
}

That being said, if the user actually did type in System.Int32 rather than int (which of course would be perfectly legal) this technique would still print out "int". There s not a whole lot you can do about that because the System.Type is the same either way -- so there s no way for you to find out which variant the user actually typed.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签