void Main()
{
test((int?)null);
test((bool?)null);
test((DateTime?)null);
}
void test(object p)
{
//**How to get the type of parameter p**
}
Run Code Online (Sandbox Code Playgroud)
也许这可以帮助:
void Main()
{
test<int>(null);
test<bool>(null);
test<DateTime>(null);
}
void test<T>(Nullable<T> p)
where T : struct, new()
{
var typeOfT = typeof(T);
}
Run Code Online (Sandbox Code Playgroud)
您无法获取该类型,因为您没有传递任何值.三次调用之间没有区别.
转换null值仅对编译器选择函数的特定重载有用.由于此处没有重载函数,因此在所有三种情况下都会调用相同的函数.在没有实际传入值的情况下,您的所有函数都将看到一个null值,它无法确定调用者将该null值转换为的类型.
| 归档时间: |
|
| 查看次数: |
151 次 |
| 最近记录: |