Search code examples
c#genericsstaticmethodsdata-access

Generics in c# & accessing the static members of T


My question concerns c# and how to access Static members ... Well I don't really know how to explain it (which kind of is bad for a question isn't it?) I will just give you some sample code:

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()
     }
}

So thank you guys for your answers (By the way the question is: how would I solve this problem without getting an error). This probably quite an easy question for you!


Edit: Thank you all for your answers!

Though I think the try - catch phrase is the most elegant, I know from my experience with vb that it can really be a bummer. I used it once and it took about 30 minutes to run a program, which later on only took 2 minutes to compute just because I avoided try - catch.

This is why I chose the switch statement as the best answer. It makes the code more complicated but on the other hand I imagine it to be relatively fast and relatively easy to read. (Though I still think there should be a more elegant way ... maybe in the next language I learn)


Though if you have some other suggestion I am still waiting (and willing to participate)


Solution

  • One more way to do it, this time some reflection in the mix:

    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;
        }
    }
    

    The next step would be trying to implement

    public static TRet CallStaticMethod<TRet>( object obj, string methodName, params object[] args );
    

    With full parameter type matching etc.