ソースコード
using System;
namespace Kogane
{
public static class TypeUtils
{
public static string GetCSharpTypeKeyword( Type type )
{
return GetCSharpTypeKeyword( type.Name );
}
public static string GetCSharpTypeKeyword( string typeName )
{
return typeName switch
{
"Boolean" => "bool",
"Byte" => "byte",
"SByte" => "sbyte",
"Char" => "char",
"Decimal" => "decimal",
"Double" => "double",
"Single" => "float",
"Int32" => "int",
"UInt32" => "uint",
"Int64" => "long",
"UInt64" => "ulong",
"Int16" => "short",
"UInt16" => "ushort",
"Object" => "object",
"String" => "string",
"Void" => "void",
_ => typeName,
};
}
}
}
通常
Debug.Log( typeof( int ).Name );
Debug.Log( typeof( float ).Name );
Debug.Log( typeof( string ).Name );
使用例
Debug.Log( TypeUtils.GetCSharpTypeKeyword( typeof( int ) ) );
Debug.Log( TypeUtils.GetCSharpTypeKeyword( typeof( float ) ) );
Debug.Log( TypeUtils.GetCSharpTypeKeyword( typeof( string ) ) );