NOTE . This question is about the technical aspect and NOT about design decisions. An answer or comment about another design DOES NOT answer this question, since I am only interested in the nature of the technical aspect of this particular question.
As C# 6.0I have this technique where I give IEnumerable<T>:
public void MyMethod(IEnumerable<object> list) { ... }
Say the caller calls it in an array MyClass[].
What I want is the class name of the generic type definition, I had this method implementation:
public void MyMethod(IEnumerable<object> list)
{
...
var name =
from abstraction in list.GetType().GetInterfaces()
where abstraction.IsGenericType
&& abstraction.GetGenericTypeDefinition() == typeof(IEnumerable<>)
from genericArgumentType in abstraction.GetGenericArguments()
select genericArgumentType.Name;
...
}
( ) (, "MyClass"). :
public static string GetGenericTypeDefinitionName<T>(this IEnumerable<T> list)
{
var name =
from abstraction in list.GetType().GetInterfaces()
where abstraction.IsGenericType
&& abstraction.GetGenericTypeDefinition() == typeof(IEnumerable<>)
from genericArgumentType in abstraction.GetGenericArguments()
select genericArgumentType.Name;
return name.Single();
}
GetGenericTypeDefinitionName() MyMethod() :
public void MyMethod(IEnumerable<object> list)
{
...
var name = list.GetGenericTypeDefinitionName();
...
}
, : " ! return typeof(T).Name ? '
, "Object", (, "MyClass") .
? , T.
? C#?