Jak wywołać metodę ogólną, jeśli znasz tylko typ parametru w czasie wykonywania?
Mam tę metodę:
<code>public List<T> SomeMethod<T>( params ) where T : new() </code>
Więc chcę to nazwaćSomeMethod
co jest w porządku, jeśli znam typ:
<code>SomeMethod<Class1>(); </code>
Ale jeśli tylko mamClass1
w czasie wykonywania nie mogę go wywołać?
Więc jak zadzwonićSomeMethod
z nieznanym typem T? Dostałem typ używając odbicia.
Mam typ typu, aleSomeMethod<Type | GetType()>
nie działa.
Aktualizacja 7. Maj:
Oto przykładowy kod tego, co chcę osiągnąć:
<code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication63 { public class DummyClass { } public class Class1 { public string Name; } class AssemblyTypesReflection { static void Main(string[] args) { object obj = new Class1() { Name = "John" } ; Assembly assembly = Assembly.GetExecutingAssembly(); var AsmClass1 = (from i in assembly.GetTypes() where i.Name == "Class1" select i).FirstOrDefault(); var list = SomeMethod<AsmClass1>((AsmClass1)obj); //Here it fails } static List<T> SomeMethod<T>(T obj) where T : new() { return new List<T> { obj }; } } } </code>
To demo wyjęte z większego kontekstu.