How to find the return type in C#?
I have doubt regarding getting the return type's .
class SampleOnOUTandREF
{
static void Main(string[] args)
{
Console.Write(typeof(int));
Console.Read();
}
}
If i write this the output is System.Int32.
But in my application i want the result as "int" if i give "System.Int32". Please help regarding this issue.In my application i am using System.Reflection for reading .dl开发者_如何学Gol's .
int is a C#-specific keyword.
The Reflection library is language-neutral and is unaware of C#-specific keywords.
Instead, you can write a Dictionary<Type, string> to contain the C# keyword types.
Section 4.1.4 of the C# spec lists all of the value types that C# has reserved words for and 4.2 mentions the classes that have aliases:
object: System.Object string: System.String sbyte: System.SByte byte: System.Byte short: System.Int16 ushort: System.UInt16 int: System.Int32 uint: System.UInt32 long: System.Int64 ulong: System.UInt64 char: System.Char float: System.Single double: System.Double bool: System.Boolean decimal: System.Decimal
As SLaks mentions, you can have a dictionary mapping CLR types to C# types, like this:
new Dictionary<string, string> {
{ "System.SByte", "sbyte" },
{ "System.Byte", "byte" },
...
or like this:
new Dictionary<Type, string> {
{ typeof(sbyte), "sbyte" },
{ typeof(byte), "byte" },
....
I will reword what others have said; I still think I can add value:.
int is not the name of a type; it is a C# keyword that the compiler treats as a synonym for the type System.Int32. You can think it this way: once your code is compiled, all references to int are gone, and replaced by references to System.Int32. System.Int32 is part of the CTS, and that is what typeof or GetType will refer to.
The C# keyword int is just an alias for System.Int32. Similarly, bool is an alias for System.Boolean and string is an alias for System.String.
Since these are all aliases and not actual types you will never see them returned by reflection. However, the types that are returned are completely equivalent.
加载中,请稍侯......
精彩评论