views:

376

answers:

3

Hi,

is it possible to call with reflection a method with "explict type argument" <S> definition
e.g. oObject.Cast<S>() ?

where is:

IList <P> oObject = new List <P>();

I tried with

oObject.getType().InvokeMember( "Cast", BindingFlags.InvokeMethod, null, oObject, null)

but it does not work, does anyone know why?

+1  A: 

I think you're looking for Type.MakeGenericType

Jamiec
I am sorry but I do not understand such short explanation, could you please write some code in order to understand it better ?
milan
+8  A: 

The Cast extension method lives on the class Enumerable, and you need to call MakeGenericMethod:

typeof(System.Linq.Enumerable)
    .GetMethod("Cast", new []{typeof(System.Collections.IEnumerable)})
    .MakeGenericMethod(typeof(S))
    .Invoke(null, new object[] { oObjectType })

update: Because the method is static, the first parameter to Invoke should be null

Rob Fonseca-Ensor
Invoke function does not take one parameter so if I put second parameter as "null"I am getting exception. any clue why ?
milan
Exception getting is:Invalid number of parameters.
milan
`Enumerable.Cast<T>()` does not take any "arguments". The argument given in this case is the object you want to call the extension method Cast on.
sixlettervariables
but there is no Invoke with just one parameter!?
milan
I must give 2 or more parameters...
milan
@milan: Apologies, it appears @Rob's code is slightly incorrect. You would call `.Invoke(null, oObject)` since Cast is static, per MSDN. http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx
sixlettervariables
please see below for comments it still does not work....
milan
it's `.Invoke(null, oObject)` not `.Invoke(oObject, null)`
Rob Fonseca-Ensor
Also can I recommend that whenever you get an exception, you tell us what the exception is (exactly) and also, if you need to update your question don't add a new "answer" - just edit the old question
Rob Fonseca-Ensor
I dont know where to write any more :) it just does not work in any combination.If possible please try code which I copied down(because code is too big to fit this small edit box). Combination you propose is not compilable! so Invoke(null, oObject) is not compilableany more ideas? or we should just quit ?
milan
IT WORKS!!!!sorry for asking so much qestions, the answer is that Invoke should have list in which the only element is oObjectType. I tried always without list and failed. Many thans to all !d.Invoke(null, new object[] { oObjectType })br,Milan.
milan
Thanks Milan, i'll update my answer
Rob Fonseca-Ensor
A: 

Hi, Here is the complete test code but still it does not work. The last line produce always exception. Is it possible to make it work ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace reflection_tester
{
    class CBase
    {
        public string Ja = "I am the base";
    }

    class MyClass01 : CBase
    {
        public string _ID;

        public string ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
    }

    class Program
    {

        public static object wrapper()
        {
            //return a list of classes MyClass01

            IList<MyClass01> lstClass01 = new List<MyClass01>();

            MyClass01 oClass01a = new MyClass01();
            oClass01a.ID = "1";

            MyClass01 oClass01b = new MyClass01();
            oClass01b.ID = "2";

            lstClass01.Add(oClass01a);
            lstClass01.Add(oClass01b);

            return lstClass01;
        }

        static void Main(string[] args)
        {

            MyClass01 oMy1 = new MyClass01();
            oMy1._ID = "1";

            MyClass01 oMy2 = new MyClass01();
            oMy2._ID = "3";

            IList<MyClass01> oListType01 = new List<MyClass01>();

            oListType01.Add(oMy1);
            oListType01.Add(oMy2);

            object oObjectType = new object();

            oObjectType = oListType01;

            /* this works */
            IEnumerable<CBase> enumList = oListType01.Cast<CBase>();

            MethodInfo mInfo = typeof(System.Linq.Enumerable).GetMethod("Cast", new[] { typeof(System.Collections.IEnumerable) }).MakeGenericMethod(typeof(CBase));

            /* this does not work, why ? throws exception */
            IEnumerable<CBase> enumThroughObject = (IEnumerable<CBase>)mInfo.Invoke(oObjectType, null);

            return;
        }
    }
}
milan