如何确定一个对象类型是一个内置的系统类型
我写一个简单的名单,其中,T>
以CSV转换器。我将检查所有的 T
的名单中,并抓住所有的公共属性和它们放到CSV。
I am writing a simple List<t>
to CSV converter. My converter checks the all the t
's in List and grabs all public properties and places them into the CSV.
我的code的伟大工程(如预期),当您将使用一个简单类的一些属性。
My code works great (as intended) when you will use a simple class with a few properties.
我想获得的名单,其中,T&GT;
来CSV转换器也接受了系统的类型,比如字符串和整数。有了这些系统类型,我不想让自己的公共属性(如长度,字符数等)。因此,我想检查,如果对象是一个系统类型。按系统类型我的意思是一个内置的.NET类型,例如字符串,INT32,双
等。
I would like to get the List<t>
to CSV converter to also accept the System types such as String and Integer. With these system types I do not want to get their public properties (such as Length, Chars etc). Thus I would like to check if the object is a System type. By System type I mean one of the built in .Net types such as string, int32, double
etc.
使用的GetType()我可以找到以下内容:
Using GetType() I can find out the following:
string myName = "Joe Doe";
bool isPrimitive = myName.GetType().IsPrimitive; // False
bool isSealed = myName.GetType().IsSealed; // True
// From memory all of the System types are sealed.
bool isValueType = myName.GetType().IsValueType; // False
// LinqPad users: isPrimitive.Dump();isSealed.Dump();isValueType.Dump();
我怎么能找到,如果MyName变量是一个内置的系统类型? (假设我们不知道它的字符串)
How can I find if variable myName is a built in System type? (assuming we don't know its a string)
myName.GetType()。命名空间
这将返回系统,如果它是一个内置式。
myName.GetType().Namespace
This will return System if it is an in-built type.