C#ェリスト <T> どのタイプのイラストのTシャツです![重複]
-
20-08-2019 - |
質問
この質問に答えはこちら
ように反射プロジェクト、そして今は滞っております。している場合はオブジェクトの"myclass"を把持することが可能なリスト というかを取得しタイプとして、以下のコードの場合は物件きます。SomListはが空っぽになってはいませんか?
List<myclass> myList = dataGenerator.getMyClasses();
lbxObjects.ItemsSource = myList;
lbxObjects.SelectionChanged += lbxObjects_SelectionChanged;
private void lbxObjects_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Reflect();
}
Private void Reflect()
{
foreach (PropertyInfo pi in lbxObjects.SelectedItem.GetType().GetProperties())
{
switch (pi.PropertyType.Name.ToLower())
{
case "list`1":
{
// This works if the List<T> contains one or more elements.
Type tTemp = GetGenericType(pi.GetValue(lbxObjects.SelectedItem, null));
// but how is it possible to get the Type if the value is null?
// I need to be able to create a new object of the type the generic list expect.
// Type type = pi.getType?? // how to get the Type of the class inside List<T>?
break;
}
}
}
}
private Type GetGenericType(object obj)
{
if (obj != null)
{
Type t = obj.GetType();
if (t.IsGenericType)
{
Type[] at = t.GetGenericArguments();
t = at.First<Type>();
} return t;
}
else
{
return null;
}
}
解決
Type type = pi.PropertyType;
if(type.IsGenericType && type.GetGenericTypeDefinition()
== typeof(List<>))
{
Type itemType = type.GetGenericArguments()[0]; // use this...
}
<時間>
より一般的には、任意のIList<T>
をサポートするために、あなたがインターフェースをチェックする必要があります:
foreach (Type interfaceType in type.GetInterfaces())
{
if (interfaceType.IsGenericType &&
interfaceType.GetGenericTypeDefinition()
== typeof(IList<>))
{
Type itemType = type.GetGenericArguments()[0];
// do something...
break;
}
}
他のヒント
私はを決定することができますどのように私はIList<>
のいくつかの種類であることを疑うオブジェクト、考えるのどのようなのそれはだType
?の
ここで勇敢なソリューションです。それはあなたがテストに実際のオブジェクト(というよりも、<=>)を前提としています。
public static Type ListOfWhat(Object list)
{
return ListOfWhat2((dynamic)list);
}
private static Type ListOfWhat2<T>(IList<T> list)
{
return typeof(T);
}
使用例:
object value = new ObservableCollection<DateTime>();
ListOfWhat(value).Dump();
プリント
typeof(DateTime)
マルクの答えは、私はこのために使用するアプローチですが、次のようなものを持っている場合はシンプル(と友好API?)のためにあなたは、コレクションの基本クラスのプロパティを定義することができます:
public abstract class CollectionBase<T> : IList<T>
{
...
public Type ElementType
{
get
{
return typeof(T);
}
}
}
私は、このアプローチが有用であることが判明し、ジェネリック医薬品への新規参入のためのわかりやすいきます。
私はを決定することができますどのように私はIList<>
のいくつかの種類であることを疑うオブジェクト、考えるのどのようなのそれはだ<=>?の
ここで信頼性の高いソリューションです。長さのための私の謝罪 - C#のイントロスペクションAPIは、これは驚くほど困難に
。/// <summary>
/// Test if a type implements IList of T, and if so, determine T.
/// </summary>
public static bool TryListOfWhat(Type type, out Type innerType)
{
Contract.Requires(type != null);
var interfaceTest = new Func<Type, Type>(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>) ? i.GetGenericArguments().Single() : null);
innerType = interfaceTest(type);
if (innerType != null)
{
return true;
}
foreach (var i in type.GetInterfaces())
{
innerType = interfaceTest(i);
if (innerType != null)
{
return true;
}
}
return false;
}
使用例:
object value = new ObservableCollection<int>();
Type innerType;
TryListOfWhat(value.GetType(), out innerType).Dump();
innerType.Dump();
を返します。
True
typeof(Int32)
所属していません StackOverflow