[英]Get the type from a nullable variable
public class MyType
{
public int? MyId { get; set; }
}
MyType myType = new MyType();
myType.MyId.GetType()
the last line returns an exception since MyId
is not set (ie. it's null). 最后一行返回一个异常,因为未设置
MyId
(即它为空)。 How I get the type ( int?
or even int
) in this case? 在这种情况下我如何得到类型(
int?
或甚至int
)? Note, int?
注意,
int?
is used as an example, the variable may have any type, this is just a simplified example. 作为一个例子,变量可以有任何类型,这只是一个简化的例子。
Note, according to Microsoft, this is supposed to work: 请注意,根据微软的说法,这应该有效:
int? a = 17;
Type typeOfA = a.GetType();
Console.WriteLine(typeOfA.FullName);
// Output:
// System.Int32
and it does work when the value is assigned... 并且在分配值时它确实有效...
EDIT. 编辑。
Looking at some of the replies and comments, I would like to add that in code, I pass myType.MyId as an object to a method that needs to figure out its type. 看一些回复和评论,我想在代码中添加,我将myType.MyId作为对象传递给需要弄清楚其类型的方法。 Basically it looks similar to:
基本上它看起来类似于:
public void RunQuery(string sql, List<(string parameterName, object parameterValue)> parameters)
so myType.MyId is passed into RunQuery as parameterValue 所以myType.MyId作为parameterValue传递给RunQuery
You can use reflection to get declared type of a property (which is known at compile time): 您可以使用反射来获取属性的声明类型(在编译时已知):
Type t = typeof(MyType).GetProperty(nameof(MyType.MyId)).PropertyType;
And GetType()
is used to figure out the actual type of an object in runtime, but that does not make sense for a null
reference. GetType()
用于计算运行时对象的实际类型,但这对于null
引用没有意义。
Edit: 编辑:
When you cast Nullable<T>
to an Object
, its value is boxed, so, if it was null
, you will get just an Object
variable with null
reference, and you won't be able to find out the type any more. 当您将
Nullable<T>
转换为Object
,其值将被加框,因此,如果它为null
,您将只获得一个带有null
引用的Object
变量,并且您将无法再找到该类型。
So, you should somehow change your infrastructure to make the type be passed with your parameter. 因此,您应该以某种方式更改基础结构,以使类型与您的参数一起传递。 The fastest workaround is to pass it explicitly
最快的解决方法是明确传递它
List<(string parameterName, object parameterValue, Type parameterType)> parameters
Check out System.Data.SqlClient.SqlParameter , I am not sure, but this is probably exactly what you need to use. 检查System.Data.SqlClient.SqlParameter ,我不确定,但这可能正是你需要使用的。
[Edited], I've re-written my solution to address your question: Making a function that is agnostic to MyType: [已编辑],我已经重新编写了我的解决方案来解决您的问题:创建一个与MyType无关的函数:
string GetPropertyName<T>(T prop)
{
var type = typeof(T);
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return type.GenericTypeArguments[0].FullName;
}
else
{
return type.FullName;
}
}
You can now call it like this: 您现在可以这样称呼它:
MyType myType = new MyType();
string name = GetPropertyName(myType.MyId);
Console.WriteLine(name); // Outputs System.Int32
I've test it, and it's working for me. 我测试了它,它对我有用。
BTW, if you pass it a non-nullable member, it will return that property name. 顺便说一句,如果你传递一个不可为空的成员,它将返回该属性名称。 You can test that for yourself.
你可以自己测试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.