일반 클래스 또는 메서드의 멤버로부터 T 유형을 가져오는 방법
클래스 또는 메서드에 다음과 같은 일반 구성원이 있다고 가정합니다.
public class Foo<T>
{
public List<T> Bar { get; set; }
public void Baz()
{
// get type of T
}
}
제가 수업을 인스턴스화할 때,T
된다MyTypeObject1
클래스에 일반 목록 속성이 있습니다.List<MyTypeObject1>
비일반 클래스의 일반 메서드에도 동일하게 적용됩니다.
public class Foo
{
public void Bar<T>()
{
var baz = new List<T>();
// get type of T
}
}
우리 반 목록에 어떤 종류의 물건이 들어 있는지 알고 싶습니다.그래서 어떤 유형의T
목록 속성이 다음과 같이 됩니까?Bar
또는 로컬 변수baz
포함?
할 수 없습니다Bar[0].GetType()
목록에 0개의 요소가 포함되어 있을 수 있기 때문입니다.어떻게 해야 하나요?
제가 정확히 이해한 바로는, 당신의 목록은 컨테이너 클래스 자체와 같은 유형 매개변수를 가지고 있습니다.이 경우 다음을 수행합니다.
Type typeParameterType = typeof(T);
만약 당신이 운이 좋은 상황에 있다면,object
형식 매개 변수로, 마크의 대답을 참조하십시오.
(참고: 당신이 알고 있는 것은object
또는IList
또는 유사하며, 런타임에 목록이 모든 유형일 수 있음)
만약 당신이 그것을 안다면.List<T>
그러면:
Type type = abc.GetType().GetGenericArguments()[0];
또 다른 옵션은 인덱서를 살펴보는 것입니다.
Type type = abc.GetType().GetProperty("Item").PropertyType;
새 유형 정보 사용:
using System.Reflection;
// ...
var type = abc.GetType().GetTypeInfo().GenericTypeArguments[0];
다음 확장 방법을 사용하면 반사 없이 벗어날 수 있습니다.
public static Type GetListType<T>(this List<T> _)
{
return typeof(T);
}
또는 더 일반적:
public static Type GetEnumeratedType<T>(this IEnumerable<T> _)
{
return typeof(T);
}
용도:
List<string> list = new List<string> { "a", "b", "c" };
IEnumerable<string> strings = list;
IEnumerable<object> objects = list;
Type listType = list.GetListType(); // string
Type stringsType = strings.GetEnumeratedType(); // string
Type objectsType = objects.GetEnumeratedType(); // BEWARE: object
해라
list.GetType().GetGenericArguments()
전체 유형 변수가 필요하지 않고 유형만 확인하려는 경우 임시 변수를 쉽게 만들고 다음을 사용할 수 있습니다.is
교환입니다.
T checkType = default(T);
if (checkType is MyClass)
{}
다음은 저에게 도움이 됩니다.여기서 myList는 알 수 없는 종류의 목록입니다.
IEnumerable myEnum = myList as IEnumerable;
Type entryType = myEnum.AsQueryable().ElementType;
제네릭 목록의 반환 유형에는 다음과 같은 항목을 사용할 수 있습니다.
public string ListType<T>(T value)
{
var valueType = value.GetType().GenericTypeArguments[0].FullName;
return valueType;
}
그GetGenericArgument()
메서드는 인스턴스의 기본 유형(클래스가 일반 클래스인 경우)에 설정해야 합니다.myClass<T>
. . 그렇지 않으면 유형 [0]을 반환합니다.
예:
Myclass<T> instance = new Myclass<T>();
Type[] listTypes = typeof(instance).BaseType.GetGenericArguments();
이 확장 방법을 사용하여 비슷한 작업을 수행합니다.
public static string GetFriendlyTypeName(this Type t)
{
var typeName = t.Name.StripStartingWith("`");
var genericArgs = t.GetGenericArguments();
if (genericArgs.Length > 0)
{
typeName += "<";
foreach (var genericArg in genericArgs)
{
typeName += genericArg.GetFriendlyTypeName() + ", ";
}
typeName = typeName.TrimEnd(',', ' ') + ">";
}
return typeName;
}
public static string StripStartingWith(this string s, string stripAfter)
{
if (s == null)
{
return null;
}
var indexOf = s.IndexOf(stripAfter, StringComparison.Ordinal);
if (indexOf > -1)
{
return s.Substring(0, indexOf);
}
return s;
}
다음과 같이 사용합니다.
[TestMethod]
public void GetFriendlyTypeName_ShouldHandleReallyComplexTypes()
{
typeof(Dictionary<string, Dictionary<string, object>>).GetFriendlyTypeName()
.ShouldEqual("Dictionary<String, Dictionary<String, Object>>");
}
이것은 당신이 찾고 있는 것은 아니지만, 관련 기술을 시연하는 데 도움이 됩니다.
고려 사항:
동일한 방법으로 20개의 입력된 목록을 내보내는 데 사용합니다.
private void Generate<T>()
{
T item = (T)Activator.CreateInstance(typeof(T));
((T)item as DemomigrItemList).Initialize();
Type type = ((T)item as DemomigrItemList).AsEnumerable().FirstOrDefault().GetType();
if (type == null)
return;
if (type != typeof(account)) // Account is listitem in List<account>
{
((T)item as DemomigrItemList).CreateCSV(type);
}
}
IEnumberable을 구현하는 모든 컬렉션 유형에서 "T" 유형을 가져올 수 있습니다.<다음을 포함합니다.
public static Type GetCollectionItemType(Type collectionType)
{
var types = collectionType.GetInterfaces()
.Where(x => x.IsGenericType
&& x.GetGenericTypeDefinition() == typeof(IEnumerable<>))
.ToArray();
// Only support collections that implement IEnumerable<T> once.
return types.Length == 1 ? types[0].GetGenericArguments()[0] : null;
}
IEnumberable을 구현하는 컬렉션 유형은 지원하지 않습니다. <예를 들어, 두 번)
public class WierdCustomType : IEnumerable<int>, IEnumerable<string> { ... }
지원이 필요하다면 여러 유형을 반환할 수 있을 것 같습니다.
또한 이 작업을 많이 수행하는 경우(예: 루프) 수집 유형별로 결과를 캐시할 수도 있습니다.
3dGrabber 솔루션 사용:
public static T GetEnumeratedType<T>(this IEnumerable<T> _)
{
return default(T);
}
//and now
var list = new Dictionary<string, int>();
var stronglyTypedVar = list.GetEnumeratedType();
public bool IsCollection<T>(T value){
var valueType = value.GetType();
return valueType.IsArray() || typeof(IEnumerable<object>).IsAssignableFrom(valueType) || typeof(IEnumerable<T>).IsAssignableFrom(valuetype);
}
속성의 기본 유형을 알고 싶다면 다음을 시도하십시오.
propInfo.PropertyType.UnderlyingSystemType.GenericTypeArguments[0]
제가 한 일은 다음과 같습니다.
internal static Type GetElementType(this Type type)
{
// Use type.GenericTypeArguments if it exists
if (type.GenericTypeArguments.Any())
return type.GenericTypeArguments.First();
return type.GetRuntimeProperty("Item").PropertyType);
}
그런 다음 이렇게 부릅니다.
var item = Activator.CreateInstance(iListType.GetElementType());
또는
var item = Activator.CreateInstance(Bar.GetType().GetElementType());
이거 먹어봐요.
if (type of(T) == type of(Person)
유형:
type = list.AsEnumerable().SingleOrDefault().GetType();
언급URL : https://stackoverflow.com/questions/557340/how-to-get-the-type-of-t-from-a-member-of-a-generic-class-or-method
'itsource' 카테고리의 다른 글
Mongo에서 컬렉션의 인덱스를 표시하려면 어떻게 해야 합니까? (0) | 2023.05.22 |
---|---|
MySQL과 같은 targzip mongo 덤프 (0) | 2023.05.22 |
일상적인 사용을 위해 zsh로 전환할 가치가 있습니까? (0) | 2023.05.22 |
Git 푸시 결과가 "인증 실패"로 표시됨 (0) | 2023.05.22 |
각선미와의 싸움JS가 컨트롤러를 두 번 실행 (0) | 2023.05.22 |