データ型 (文字列、日時、10 進数など) を格納できるコレクションを作成するにはどうすればよいですか? そして、このコレクションを使用して比較を実行します。
if (pi.PropertyType.IsIn ([how can check against the collection of type) //where pi is a property info]))
{
}
助言がありますか?
データ型 (文字列、日時、10 進数など) を格納できるコレクションを作成するにはどうすればよいですか? そして、このコレクションを使用して比較を実行します。
if (pi.PropertyType.IsIn ([how can check against the collection of type) //where pi is a property info]))
{
}
助言がありますか?
List<Type> types = new List<Type> {typeof(string), typeof(int)};
if (types.Contains(pi.PropertyType))
{
//do stuff
}
うまくいけば、私はあなたの質問を正しく理解しています。最初に、コレクション型のより正確な定義が必要です。これが私が使用するものです:
コレクションはジェネリックです
IList<T>コレクションは、ICollection<T>やなどの標準コレクション インターフェイスの 1 つを実装していますIEnumerable<T>。この例では、コレクション型は から派生した型IEnumerable<T>です。
型がコレクションであるかどうかを検出するには、を使用して実装するインターフェイスを取得する必要がありGetInterfaces()ます。IsGenericTypeIList<String>IList<>GetGenericTypeDefinition()
var genericCollectionType = typeof(IEnumerable<>);
var isCollection = pi
.PropertyType
.GetInterfaces()
.Where(type => type.IsGenericType)
.Select(type => type.GetGenericTypeDefinition())
.Contains(genericCollectionType);
if (isCollection) {
...
}