locked
Casting object if Type known RRS feed

  • Question

  • Hello All,

    I am using reflection to dynamically invoke a function. Is there a way to cast the result ?
    e.g.

    Type
    MyType = Type.GetType("Workbook.PQClass");
    MethodInfo MyTypeMethod = MyType.GetMethod("HelloWorld");
    Type RetType = MyTypeMethod.GetType();
    RetValue = MyTypeMethod.Invoke(MyPQClass,
    null);

    Can I cast RetValue to RetType ?

    Thanks in advance 

     



    Monday, April 20, 2009 6:11 PM

Answers

  • Unfortunately, you're running into a limitation of Reflection.  Once you enter reflection, you're stuck in reflection-land.  It's logically impossible to get back to a strongly-typed environment without creating an if/then for each possible return value.  Nevertheless, RetValue is of the type "object" more than likely, so there's no way to get it out of there without hard-coding a cast, so your only solution is something like (int)RetValue, which doesn't do you much good, because you're not sure if it's going to be an int or not.  I'm afraid there's no pretty way out of this predicament.
    David Morton - http://blog.davemorton.net/
    • Proposed as answer by Guo Surfer Wednesday, April 22, 2009 10:11 AM
    • Marked as answer by Guo Surfer Saturday, April 25, 2009 4:10 PM
    Monday, April 20, 2009 6:29 PM
  • The return is an object, you can cast it...but if you know the type of object being run, why not use that knowledge switch off of the string class name and then apply the as keyword such as below:


    public class DynamicClass
    {
    
        public static void DoIt()
        {
    
            string targetType = "MSDN_ULA.DynamicClass";
            Type reflectedType = Type.GetType(targetType);
    
            object obj = Activator.CreateInstance(reflectedType);
            DynamicClass theClass = obj as DynamicClass;
    
            if ( theClass != null )
                Console.WriteLine( theClass.ReturnIntMethod() );
            else
                Console.WriteLine( "Reflection Failure!" );
    
        }
    
        public int ReturnIntMethod() { return 41; }
    
    
    }



    William Wegerson (www.OmegaCoder.Com)
    • Edited by OmegaMan Monday, April 20, 2009 7:51 PM Code formatted badly...redo
    • Proposed as answer by Guo Surfer Wednesday, April 22, 2009 10:11 AM
    • Marked as answer by Guo Surfer Saturday, April 25, 2009 4:10 PM
    Monday, April 20, 2009 7:49 PM

All replies

  • Unfortunately, you're running into a limitation of Reflection.  Once you enter reflection, you're stuck in reflection-land.  It's logically impossible to get back to a strongly-typed environment without creating an if/then for each possible return value.  Nevertheless, RetValue is of the type "object" more than likely, so there's no way to get it out of there without hard-coding a cast, so your only solution is something like (int)RetValue, which doesn't do you much good, because you're not sure if it's going to be an int or not.  I'm afraid there's no pretty way out of this predicament.
    David Morton - http://blog.davemorton.net/
    • Proposed as answer by Guo Surfer Wednesday, April 22, 2009 10:11 AM
    • Marked as answer by Guo Surfer Saturday, April 25, 2009 4:10 PM
    Monday, April 20, 2009 6:29 PM
  • The return is an object, you can cast it...but if you know the type of object being run, why not use that knowledge switch off of the string class name and then apply the as keyword such as below:


    public class DynamicClass
    {
    
        public static void DoIt()
        {
    
            string targetType = "MSDN_ULA.DynamicClass";
            Type reflectedType = Type.GetType(targetType);
    
            object obj = Activator.CreateInstance(reflectedType);
            DynamicClass theClass = obj as DynamicClass;
    
            if ( theClass != null )
                Console.WriteLine( theClass.ReturnIntMethod() );
            else
                Console.WriteLine( "Reflection Failure!" );
    
        }
    
        public int ReturnIntMethod() { return 41; }
    
    
    }



    William Wegerson (www.OmegaCoder.Com)
    • Edited by OmegaMan Monday, April 20, 2009 7:51 PM Code formatted badly...redo
    • Proposed as answer by Guo Surfer Wednesday, April 22, 2009 10:11 AM
    • Marked as answer by Guo Surfer Saturday, April 25, 2009 4:10 PM
    Monday, April 20, 2009 7:49 PM