2

私は以下のようなクラスを持っています:-

public Class Student
{
  string Name {get; set;}
  int Age {get; set;}
  Address studentAddres {get; set;}
}

public class Address
{
 string Street{get; set;}
 string City {get; set;}
}

ここで、Name と AgeSystem Define タイプで、StudentAddresカスタム タイプです。コードを使用してそれらを区別する方法。リフレクションを使用していますが、達成できません。

4

2 に答える 2

2
if (SomeObject.GetType().Assembly != typeof(int).Assembly)
{
    //SomeObject is defined as part of my program
}
else
{
    //SomeObject is a standard .Net type
}
于 2012-12-11T11:57:04.193 に答える
1

値型か文字列のどちらかを見たいようです。次に使用できます: Type.IsPrimitiveプロパティ

Type がプリミティブ型の 1 つであるかどうかを示す値を取得します。

プリミティブ型は、Boolean、Byte、SByte、Int16、UInt16、Int32、UInt32、Int64、UInt64、IntPtr、UIntPtr、Char、Double、および Single です。

int i = 10;
string str = "";
var isPrimitive = i.GetType().IsValueType || i is string; // returns true since i is value type
var isPrimitiveWithString = str.GetType().IsValueType || str is string; 
 // returns true

CustomClass obj = new CustomClass();
var isPrimitive3 = obj.GetType().IsPrimitive; // returns false
于 2012-12-11T12:16:04.653 に答える