Invoking "Explicit type argument" methods with reflection ? Possib

M

Milan

Hi all,

I need to reimplement the following function call "Cast<IMyClass>()" using
reflection:
IEnumerable<IMyClass> tMyList = wrapper ( ).Cast<IMyClass>();

declaration of wrapper is:
object wrapper ( );

wrapper() method returns "object" which is initialized inside "wrapper" with
the object of type "IList<MyClass>"

dec.of class
class MyClass: IMyClass {}

since wrapper returns "object" which does not have directlly accesible
"Cast" method I would like with reflection to call method "Cast<IMyClass>()",
how ?
How reflection is used in the case of using "explicit type argument"
<IMyClass> ?

Br,
Milan.
 
P

Peter Duniho

Milan said:
Hi all,

I need to reimplement the following function call "Cast<IMyClass>()" using
reflection:
IEnumerable<IMyClass> tMyList = wrapper ( ).Cast<IMyClass>();

declaration of wrapper is:
object wrapper ( );

wrapper() method returns "object" which is initialized inside "wrapper" with
the object of type "IList<MyClass>"

dec.of class
class MyClass: IMyClass {}

since wrapper returns "object" which does not have directlly accesible
"Cast" method I would like with reflection to call method "Cast<IMyClass>()",
how ?
How reflection is used in the case of using "explicit type argument"
<IMyClass> ?

I don't think you need reflection. Why can't you just do this:

IEnumerable<IMyClass> tMyList =
((IEnumerable)wrapper()).Cast<IMyClass>();

?

Of course, there is also the question as to why your "wrapper()" method
isn't just returning a more sensible type than "object". But taking
that as granted, a simple cast of the return type should suffice.

Pete
 
M

Milan

Hi,

here is better documented 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 = "ja sam";
}

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

Peter Duniho

Milan said:
[...]
/* this works */
IEnumerable<CBase> enumList = oListType01.Cast<CBase>();

Sure, of course it does. As would this:

IEnumerable said:
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);

If you ask a question about why an exception occurs, you really should
post the exact name and message for the exception.

That said, looking at the code, I would guess the exception you're
getting is an ArgumentException, seeing as how even though you're
invoking a static method, you are passing the first (and only) argument
to that method as the target instance (which should be null), and
failing to pass that same reference in the parameters array (which
should be a non-null, single-element array containing that reference).

But the question remains: what is the point of using reflection here?
You don't need it (you also don't need a lot of the other code you
included, such as initializing "oObjectType" when the very next program
statement assigns a new value to it).

Pete
 
M

Milan

Hi Pete,

Exceprion says there is Parameter conflict, but VS(exception report too) is
installed on German language so that is why I did not included it here. Code
is available so exceprion was easy to reproduce. Please do not try to
understand sence of algorithm exposed here because it is just equivalent copy
of the real project beeing too big to copy/uderstand here. But if can you are
invited to come to Europe and personally to take a loók at code :)
As you said
IEnumerable<CBase> enumThroughObject =
(IEnumerable<CBase>)mInfo.Invoke(null, new object[] { oObjectType});

works now so thanks a lot. But one more question is there any good tutorial
about Invoke function ? Invoke have one set of argument for normal/member
function second for static third for extension methods.... is there any
simple rule ?


Br,
Milan
 
P

Peter Duniho

Milan said:
Exceprion says there is Parameter conflict, but VS(exception report too) is
installed on German language so that is why I did not included it here.

Options include temporarily switching your configured language to
English so that you can get the English version of the exception, or at
least reporting the actual type name of the exception (which is always
in English), or providing _some_ translation of the exception message,
or some combination of the above.
Code is available so exceprion was easy to reproduce.

That's true. That doesn't mean it's not a good idea to provide that
information yourself. Not everyone actually has a running version of
Windows with Visual Studio handy when they are replying to newsgroup
posts (for example, I'm using my Mac, and don't have Windows up at the
moment).

The less information you provide, the more likely it is you will have to
wait, sometimes indefinitely, for a useful reply. It's for your own
benefit to provide as much information as possible.
Please do not try to
understand sence of algorithm exposed here because it is just equivalent copy
of the real project beeing too big to copy/uderstand here.

If you fail to post a code example that is truly representative of the
code you're trying to deal with, then you will fail to receive answers
to your question that are truly applicable to the problem you're trying
to solve.

I find it unlikely that the design you're working with, in which you
feel it necessary to use reflection for something simple like this, is
the correct one. But there's no way for anyone to provide any useful
advice regarding better alternatives without a proper code example.
[...] But one more question is there any good tutorial
about Invoke function ? Invoke have one set of argument for normal/member
function second for static third for extension methods.... is there any
simple rule ?

Your "second" and "third" are the same. An extension method is nothing
more than a plain old static method, with some extra syntax to allow the
compiler at _compile_ time to use it as part of overload resolution.

At run-time, there's nothing special about extension methods at all.
They are just like any other static method. (In fact, at compile time
you can execute them explicitly just like any other static method too,
if you like).

Thus, there really shouldn't be any need for a tutorial on the
MethodInfo.Invoke() method. The documentation already covers all of the
specifics you need to know.

Pete
 
M

Milan

Hi Peter,

thanks for all advices but code consisting of more files is simply
impractical to sent and additionally do you realy expect that real project
are going to be exposed here?! My boss would not allowed that so I suggest
you to concentrate on the simple examples which I provide. That is how you
can help me the most. I can try to make example more real like..but I will
not send real code here. I also do not think that you should spend your time
on teaching me how to reorganize my project so simple code no metter how
ilogical should be enough.
If you have enough time I would be glad to discuss with you software
arhitectures and similar thing and we can do that online on skype,
messanger...
I decide to solve this problem without reflection so this item can be closed.
Br,
Milan.
 
P

Peter Duniho

Milan said:
Hi Peter,

thanks for all advices but code consisting of more files is simply
impractical to sent and additionally do you realy expect that real project
are going to be exposed here?!

No, of course not. That would not meet the "concise" component of the
need for a code example to be "concise-but-complete". But there's quite
a lot of territory between a code example that doesn't apply at all, and
a posting the entire project's code.
My boss would not allowed that so I suggest
you to concentrate on the simple examples which I provide.

Thanks for the suggestion, but I will continue to suggest actual,
applicable code examples, that are both concise and complete, at
practically every opportunity. As far as "concentration" goes, I have
no option but to "concentrate" on whatever sample is provided. But a
poor sample will always result in a comment to that effect from me.

If you don't like how your questions are answered, you are free to not
ask them. My comments are intended to _help you_ post a question that
will get a better answer. You comments are intended to help me, how?
They seem more defensive than helpful to others. :)
[...]
I decide to solve this problem without reflection so this item can be closed.

Just as I said was most likely possible and a better approach. I'm
happy you found my advice helpful and followed it.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top