如何在 C# 中查找对象是值类型还是引用类型

How to Find the Object is a Value Type OR Reference Types in C#

我有一个方法,它有一个类型为 object 的参数。

因为我必须找到 objectValue TypeReference Type

Public void MyMethod(object param)
{
    if(param is Value Type)
    {
        // Do Some Operation related to Value Type
    } 
    else if(param is Reference Type)
    {
        // Do Some Operation related to Reference Type
    } 
}

值类型列表

引用类型列表

您可以在 Type:

上使用属性 IsValueType and IsClass
if(param.GetType().IsValueType)
{
    // param is value type
} 
else if(param.GetType().IsClass)
{
    // param is reference type
}