Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sample
- {
- public void Example(string typeName)
- {
- Type myType = FindType(typeName);
- // what goes here to call GenericMethod<T>() ?
- GenericMethod<myType>(); // This doesn't work
- // what changes to call StaticMethod<T>() ?
- Sample.StaticMethod<myType>(); // This also doesn't work
- }
- public void GenericMethod<T>()
- {
- ...
- }
- public static void StaticMethod<T>()
- {
- ...
- }
- }
- MethodInfo method = typeof(Sample).GetMethod("GenericMethod");
- MethodInfo generic = method.MakeGenericMethod(myType);
- generic.Invoke(this, null);
- MethodInfo method = typeof(Sample).GetMethod("GenericMethod");
- MethodInfo generic = method.MakeGenericMethod(myType);
- generic.Invoke(this, null);
- Action<> GenMethod = GenericMethod<int>; //change int by any base type
- //accepted by GenericMethod
- MethodInfo method = this.GetType().GetMethod(GenMethod.Method.Name);
- MethodInfo generic = method.MakeGenericMethod(myType);
- generic.Invoke(this, null);
- class Alpha { }
- class Beta { }
- class Service
- {
- public void Process<T>(T item)
- {
- Console.WriteLine("item.GetType(): " + item.GetType()
- + "ttypeof(T): " + typeof(T));
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var a = new Alpha();
- var b = new Beta();
- var service = new Service();
- service.Process(a); //same as "service.Process<Alpha>(a)"
- service.Process(b); //same as "service.Process<Beta>(b)"
- var objects = new object[] { a, b };
- foreach (var o in objects)
- {
- service.Process(o); //same as "service.Process<object>(o)"
- }
- foreach (var o in objects)
- {
- dynamic dynObj = o;
- service.Process(dynObj); //or write "service.Process((dynamic)o)"
- }
- }
- }
- item.GetType(): Alpha typeof(T): Alpha
- item.GetType(): Beta typeof(T): Beta
- item.GetType(): Alpha typeof(T): System.Object
- item.GetType(): Beta typeof(T): System.Object
- item.GetType(): Alpha typeof(T): Alpha
- item.GetType(): Beta typeof(T): Beta
- foreach (var o in objects)
- {
- MethodInfo method = typeof(Service).GetMethod("Process");
- MethodInfo generic = method.MakeGenericMethod(o.GetType());
- generic.Invoke(service, new object[] { o });
- }
- class Program
- {
- static void Main(string[] args)
- {
- object obj = new Alpha();
- Helper((dynamic)obj);
- }
- public static void Helper<T>(T obj)
- {
- GenericMethod<T>();
- }
- public static void GenericMethod<T>()
- {
- Console.WriteLine("GenericMethod<" + typeof(T) + ">");
- }
- }
- interface IItem { }
- class FooItem : IItem { }
- class BarItem : IItem { }
- class Program
- {
- static void Main(string[] args)
- {
- var objects = new object[] { new FooItem(), new BarItem(), new Alpha() };
- for (int i = 0; i < objects.Length; i++)
- {
- ProcessItem((dynamic)objects[i], "test" + i, i);
- //ProcesItm((dynamic)objects[i], "test" + i, i);
- //compiler error: The name 'ProcesItm' does not
- //exist in the current context
- //ProcessItem((dynamic)objects[i], "test" + i);
- //error: No overload for method 'ProcessItem' takes 2 arguments
- }
- }
- static string ProcessItem<T>(T item, string text, int number)
- where T : IItem
- {
- Console.WriteLine("Generic ProcessItem<{0}>, text {1}, number:{2}",
- typeof(T), text, number);
- return "OK";
- }
- static void ProcessItem(BarItem item, string text, int number)
- {
- Console.WriteLine("ProcessItem with Bar, " + text + ", " + number);
- }
- }
- var result = ProcessItem((dynamic)testObjects[i], "test" + i, i);
- string result = ProcessItem((dynamic)testObjects[i], "test" + i, i);
- var name = InvokeMemberName.Create;
- Dynamic.InvokeMemberAction(this, name("GenericMethod", new[]{myType}));
- var staticContext = InvokeContext.CreateStatic;
- Dynamic.InvokeMemberAction(staticContext(typeof(Sample)), name("StaticMethod", new[]{myType}));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement