C#判断对象类型为值类型、字符串、对象引用类型或泛型
C#判断对象类型为值类型、字符串、对象引用类型或泛型
C# Code:
internal static void TestObjectType()
{
object oValue = 1; //值类型:IsValueType=True, IsClass=False
object oString = "字符串";//字符串:IsValueType=False, IsClass=True, 需要多重判断(o is String)
object oObject = new StringBuilder();//对象:IsValueType=False, IsClass=True
object oGeneric = new List<Object>();//泛型
object oRef = oGeneric;//引用类型(与被引用的对象测试一致)
Test(oValue);
Test(oString);
Test(oObject);
Test(oGeneric);
Test(oRef);
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
object oValue = 1; //值类型:IsValueType=True, IsClass=False
object oString = "字符串";//字符串:IsValueType=False, IsClass=True, 需要多重判断(o is String)
object oObject = new StringBuilder();//对象:IsValueType=False, IsClass=True
object oGeneric = new List<Object>();//泛型
object oRef = oGeneric;//引用类型(与被引用的对象测试一致)
Test(oValue);
Test(oString);
Test(oObject);
Test(oGeneric);
Test(oRef);
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
C# Code:
private static void Test(object o)
{
Type T = o.GetType();
bool isValueType = T.IsValueType;//值类型
bool isClass = T.IsClass;//类类型(对象类型)
bool isGenericType = T.IsGenericType;//泛型
bool isConstructedGenericType = T.IsConstructedGenericType;//对象为构造泛型类型
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
Type T = o.GetType();
bool isValueType = T.IsValueType;//值类型
bool isClass = T.IsClass;//类类型(对象类型)
bool isGenericType = T.IsGenericType;//泛型
bool isConstructedGenericType = T.IsConstructedGenericType;//对象为构造泛型类型
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网