code_junkie

Generics in c# & accessing the static members of T

Nov 10th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. Class test<T>{
  2. int method1(Obj Parameter1){
  3. //in here I want to do something which I would explain as
  4. T.TryParse(Parameter1);
  5.  
  6. //my problem is that it does not work ... I get an error.
  7. //just to explain: if I declare test<int> (with type Integer)
  8. //I want my sample code to call int.TryParse(). If it were String
  9. //it should have been String.TryParse()
  10. }
  11. }
  12.  
  13. static class Parser
  14. {
  15. public static bool TryParse<TType>( string str, out TType x )
  16. {
  17. // Get the type on that TryParse shall be called
  18. Type objType = typeof( TType );
  19.  
  20. // Enumerate the methods of TType
  21. foreach( MethodInfo mi in objType.GetMethods() )
  22. {
  23. if( mi.Name == "TryParse" )
  24. {
  25. // We found a TryParse method, check for the 2-parameter-signature
  26. ParameterInfo[] pi = mi.GetParameters();
  27. if( pi.Length == 2 ) // Find TryParse( String, TType )
  28. {
  29. // Build a parameter list for the call
  30. object[] paramList = new object[2] { str, default( TType ) };
  31.  
  32. // Invoke the static method
  33. object ret = objType.InvokeMember( "TryParse", BindingFlags.InvokeMethod, null, null, paramList );
  34.  
  35. // Get the output value from the parameter list
  36. x = (TType)paramList[1];
  37. return (bool)ret;
  38. }
  39. }
  40. }
  41.  
  42. // Maybe we should throw an exception here, because we were unable to find the TryParse
  43. // method; this is not just a unable-to-parse error.
  44.  
  45. x = default( TType );
  46. return false;
  47. }
  48. }
  49.  
  50. public static TRet CallStaticMethod<TRet>( object obj, string methodName, params object[] args );
  51.  
  52. class test<T>
  53. {
  54. T Method(object P)
  55. {
  56. try {
  57. return (T)Convert.ChangeType(P, typeof(T));
  58. } catch(Exception e) {
  59. return null;
  60. }
  61. }
  62. }
  63.  
  64. public class Example
  65. {
  66. internal static class Support
  67. {
  68. private delegate bool GenericParser<T>(string s, out T o);
  69. private static Dictionary<Type, object> parsers =
  70. MakeStandardParsers();
  71. private static Dictionary<Type, object> MakeStandardParsers()
  72. {
  73. Dictionary<Type, object> d = new Dictionary<Type, object>();
  74. // You need to add an entry for every type you want to cope with.
  75. d[typeof(int)] = new GenericParser<int>(int.TryParse);
  76. d[typeof(long)] = new GenericParser<long>(long.TryParse);
  77. d[typeof(float)] = new GenericParser<float>(float.TryParse);
  78. return d;
  79. }
  80. public static bool TryParse<T>(string s, out T result)
  81. {
  82. return ((GenericParser<T>)parsers[typeof(T)])(s, out result);
  83. }
  84. }
  85. public class Test<T>
  86. {
  87. public static T method1(string s)
  88. {
  89. T value;
  90. bool success = Support.TryParse(s, out value);
  91. return value;
  92. }
  93. }
  94. public static void Main()
  95. {
  96. Console.WriteLine(Test<int>.method1("23"));
  97. Console.WriteLine(Test<float>.method1("23.4"));
  98. Console.WriteLine(Test<long>.method1("99999999999999"));
  99. Console.ReadLine();
  100. }
  101. }
  102.  
  103. Class test<T>
  104. {
  105. T method1(object Parameter1){
  106.  
  107. if( Parameter1 is T )
  108. {
  109. T value = (T) Parameter1;
  110. //do something with value
  111. return value;
  112. }
  113. else
  114. {
  115. //Parameter1 is not a T
  116. return default(T); //or throw exception
  117. }
  118. }
  119. }
  120.  
  121. Class test<T>
  122. {
  123. int method1(IConvertible Parameter1){
  124.  
  125. IFormatProvider provider = System.Globalization.CultureInfo.CurrentCulture.GetFormat(typeof(T));
  126.  
  127. T temp = Parameter1.ToType(typeof(T), provider);
  128. }
  129. }
  130.  
  131. Class test<T>
  132. {
  133. int method1(object Parameter1){
  134.  
  135. if(Parameter1 is IConvertible) {
  136.  
  137. IFormatProvider provider = System.Globalization.CultureInfo.CurrentCulture.GetFormat(typeof(T));
  138.  
  139. T temp = Parameter1.ToType(typeof(T), provider);
  140.  
  141. } else {
  142. // Do something else
  143. }
  144. }
  145. }
  146.  
  147. Class a<T>{
  148. private void checkWetherTypeIsOK()
  149. {
  150. if (T is int || T is float //|| ... any other types you want to be allowed){
  151. return true;
  152. }
  153. else {
  154. throw new exception();
  155. }
  156. }
  157. public static a(){
  158. ccheckWetherTypeIsOK();
  159. }
  160. }
  161.  
  162. class test1<T> where T: struct
  163.  
  164. class MyStatic { public static int MyValue=0; }
  165. class Test<T> where T: MyStatic
  166. {
  167. public void TheTest() { T.MyValue++; }
  168. }
  169.  
  170. class a {
  171. static StaticMethod1 ()
  172. virtual Method1 ()
  173. }
  174.  
  175. class b : a {
  176. override Method1 () return StaticMethod1()
  177. }
  178.  
  179. class c : a {
  180. override Method1 () return "XYZ"
  181. }
  182.  
  183. class generic<T>
  184. where T : a {
  185. void DoSomething () T.Method1()
  186. }
Add Comment
Please, Sign In to add comment