Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. public class Sample
  2. {
  3.  
  4. public void Example(string typeName)
  5. {
  6. Type myType = FindType(typeName);
  7.  
  8. // what goes here to call GenericMethod<T>() ?
  9. GenericMethod<myType>(); // This doesn't work
  10.  
  11. // what changes to call StaticMethod<T>() ?
  12. Sample.StaticMethod<myType>(); // This also doesn't work
  13. }
  14.  
  15. public void GenericMethod<T>()
  16. {
  17. ...
  18. }
  19.  
  20. public static void StaticMethod<T>()
  21. {
  22. ...
  23. }
  24. }
  25.  
  26. MethodInfo method = typeof(Sample).GetMethod("GenericMethod");
  27. MethodInfo generic = method.MakeGenericMethod(myType);
  28. generic.Invoke(this, null);
  29.  
  30. MethodInfo method = typeof(Sample).GetMethod("GenericMethod");
  31. MethodInfo generic = method.MakeGenericMethod(myType);
  32. generic.Invoke(this, null);
  33.  
  34. Action<> GenMethod = GenericMethod<int>; //change int by any base type
  35. //accepted by GenericMethod
  36. MethodInfo method = this.GetType().GetMethod(GenMethod.Method.Name);
  37. MethodInfo generic = method.MakeGenericMethod(myType);
  38. generic.Invoke(this, null);
  39.  
  40. class Alpha { }
  41. class Beta { }
  42. class Service
  43. {
  44. public void Process<T>(T item)
  45. {
  46. Console.WriteLine("item.GetType(): " + item.GetType()
  47. + "ttypeof(T): " + typeof(T));
  48. }
  49. }
  50. class Program
  51. {
  52. static void Main(string[] args)
  53. {
  54. var a = new Alpha();
  55. var b = new Beta();
  56.  
  57. var service = new Service();
  58. service.Process(a); //same as "service.Process<Alpha>(a)"
  59. service.Process(b); //same as "service.Process<Beta>(b)"
  60.  
  61. var objects = new object[] { a, b };
  62. foreach (var o in objects)
  63. {
  64. service.Process(o); //same as "service.Process<object>(o)"
  65. }
  66. foreach (var o in objects)
  67. {
  68. dynamic dynObj = o;
  69. service.Process(dynObj); //or write "service.Process((dynamic)o)"
  70. }
  71. }
  72. }
  73.  
  74. item.GetType(): Alpha typeof(T): Alpha
  75. item.GetType(): Beta typeof(T): Beta
  76. item.GetType(): Alpha typeof(T): System.Object
  77. item.GetType(): Beta typeof(T): System.Object
  78. item.GetType(): Alpha typeof(T): Alpha
  79. item.GetType(): Beta typeof(T): Beta
  80.  
  81. foreach (var o in objects)
  82. {
  83. MethodInfo method = typeof(Service).GetMethod("Process");
  84. MethodInfo generic = method.MakeGenericMethod(o.GetType());
  85. generic.Invoke(service, new object[] { o });
  86. }
  87.  
  88. class Program
  89. {
  90. static void Main(string[] args)
  91. {
  92. object obj = new Alpha();
  93.  
  94. Helper((dynamic)obj);
  95. }
  96.  
  97. public static void Helper<T>(T obj)
  98. {
  99. GenericMethod<T>();
  100. }
  101.  
  102. public static void GenericMethod<T>()
  103. {
  104. Console.WriteLine("GenericMethod<" + typeof(T) + ">");
  105. }
  106. }
  107.  
  108. interface IItem { }
  109. class FooItem : IItem { }
  110. class BarItem : IItem { }
  111.  
  112. class Program
  113. {
  114. static void Main(string[] args)
  115. {
  116. var objects = new object[] { new FooItem(), new BarItem(), new Alpha() };
  117. for (int i = 0; i < objects.Length; i++)
  118. {
  119. ProcessItem((dynamic)objects[i], "test" + i, i);
  120.  
  121. //ProcesItm((dynamic)objects[i], "test" + i, i);
  122. //compiler error: The name 'ProcesItm' does not
  123. //exist in the current context
  124.  
  125. //ProcessItem((dynamic)objects[i], "test" + i);
  126. //error: No overload for method 'ProcessItem' takes 2 arguments
  127. }
  128. }
  129.  
  130. static string ProcessItem<T>(T item, string text, int number)
  131. where T : IItem
  132. {
  133. Console.WriteLine("Generic ProcessItem<{0}>, text {1}, number:{2}",
  134. typeof(T), text, number);
  135. return "OK";
  136. }
  137. static void ProcessItem(BarItem item, string text, int number)
  138. {
  139. Console.WriteLine("ProcessItem with Bar, " + text + ", " + number);
  140. }
  141. }
  142.  
  143. var result = ProcessItem((dynamic)testObjects[i], "test" + i, i);
  144.  
  145. string result = ProcessItem((dynamic)testObjects[i], "test" + i, i);
  146.  
  147. var name = InvokeMemberName.Create;
  148. Dynamic.InvokeMemberAction(this, name("GenericMethod", new[]{myType}));
  149.  
  150.  
  151. var staticContext = InvokeContext.CreateStatic;
  152. Dynamic.InvokeMemberAction(staticContext(typeof(Sample)), name("StaticMethod", new[]{myType}));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement