Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class test<T>{
- int method1(Obj Parameter1){
- //in here I want to do something which I would explain as
- T.TryParse(Parameter1);
- //my problem is that it does not work ... I get an error.
- //just to explain: if I declare test<int> (with type Integer)
- //I want my sample code to call int.TryParse(). If it were String
- //it should have been String.TryParse()
- }
- }
- static class Parser
- {
- public static bool TryParse<TType>( string str, out TType x )
- {
- // Get the type on that TryParse shall be called
- Type objType = typeof( TType );
- // Enumerate the methods of TType
- foreach( MethodInfo mi in objType.GetMethods() )
- {
- if( mi.Name == "TryParse" )
- {
- // We found a TryParse method, check for the 2-parameter-signature
- ParameterInfo[] pi = mi.GetParameters();
- if( pi.Length == 2 ) // Find TryParse( String, TType )
- {
- // Build a parameter list for the call
- object[] paramList = new object[2] { str, default( TType ) };
- // Invoke the static method
- object ret = objType.InvokeMember( "TryParse", BindingFlags.InvokeMethod, null, null, paramList );
- // Get the output value from the parameter list
- x = (TType)paramList[1];
- return (bool)ret;
- }
- }
- }
- // Maybe we should throw an exception here, because we were unable to find the TryParse
- // method; this is not just a unable-to-parse error.
- x = default( TType );
- return false;
- }
- }
- public static TRet CallStaticMethod<TRet>( object obj, string methodName, params object[] args );
- class test<T>
- {
- T Method(object P)
- {
- try {
- return (T)Convert.ChangeType(P, typeof(T));
- } catch(Exception e) {
- return null;
- }
- }
- }
- public class Example
- {
- internal static class Support
- {
- private delegate bool GenericParser<T>(string s, out T o);
- private static Dictionary<Type, object> parsers =
- MakeStandardParsers();
- private static Dictionary<Type, object> MakeStandardParsers()
- {
- Dictionary<Type, object> d = new Dictionary<Type, object>();
- // You need to add an entry for every type you want to cope with.
- d[typeof(int)] = new GenericParser<int>(int.TryParse);
- d[typeof(long)] = new GenericParser<long>(long.TryParse);
- d[typeof(float)] = new GenericParser<float>(float.TryParse);
- return d;
- }
- public static bool TryParse<T>(string s, out T result)
- {
- return ((GenericParser<T>)parsers[typeof(T)])(s, out result);
- }
- }
- public class Test<T>
- {
- public static T method1(string s)
- {
- T value;
- bool success = Support.TryParse(s, out value);
- return value;
- }
- }
- public static void Main()
- {
- Console.WriteLine(Test<int>.method1("23"));
- Console.WriteLine(Test<float>.method1("23.4"));
- Console.WriteLine(Test<long>.method1("99999999999999"));
- Console.ReadLine();
- }
- }
- Class test<T>
- {
- T method1(object Parameter1){
- if( Parameter1 is T )
- {
- T value = (T) Parameter1;
- //do something with value
- return value;
- }
- else
- {
- //Parameter1 is not a T
- return default(T); //or throw exception
- }
- }
- }
- Class test<T>
- {
- int method1(IConvertible Parameter1){
- IFormatProvider provider = System.Globalization.CultureInfo.CurrentCulture.GetFormat(typeof(T));
- T temp = Parameter1.ToType(typeof(T), provider);
- }
- }
- Class test<T>
- {
- int method1(object Parameter1){
- if(Parameter1 is IConvertible) {
- IFormatProvider provider = System.Globalization.CultureInfo.CurrentCulture.GetFormat(typeof(T));
- T temp = Parameter1.ToType(typeof(T), provider);
- } else {
- // Do something else
- }
- }
- }
- Class a<T>{
- private void checkWetherTypeIsOK()
- {
- if (T is int || T is float //|| ... any other types you want to be allowed){
- return true;
- }
- else {
- throw new exception();
- }
- }
- public static a(){
- ccheckWetherTypeIsOK();
- }
- }
- class test1<T> where T: struct
- class MyStatic { public static int MyValue=0; }
- class Test<T> where T: MyStatic
- {
- public void TheTest() { T.MyValue++; }
- }
- class a {
- static StaticMethod1 ()
- virtual Method1 ()
- }
- class b : a {
- override Method1 () return StaticMethod1()
- }
- class c : a {
- override Method1 () return "XYZ"
- }
- class generic<T>
- where T : a {
- void DoSomething () T.Method1()
- }
Add Comment
Please, Sign In to add comment