Does Type.GetType works with IList ?

26/08/2009 - 17:25 von Milan | Report spam
Hi comunity,

I have noticed that once IList object is created with:
IList<baza> il_baza = new List<baza>();

type of object "il_baza" is not any more "IList" but "List`1". Also I have
no clue what does it means "`1" in type definition, any idea ?

Later if I try with reflection to InvokeMember:
type.InvokeMember("ElementAt", BindingFlags.Default |
BindingFlags.InvokeMethod, null, il_baza, arg);

I get MissingMethodException error message because of type is not any more
IList:

Die Methode "System.Collections.Generic.List`1[[Reflection.baza,
ConsoleApplication2, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]].ElementAt" konnte nicht gefunden werden.

Why type has been not read out correctly like IList but List`1?

Thanks and have a nice day,
Milan.

The code is following:

class baza
{
public string vrati()
{ return "u bazi"; }
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
IList<baza> il_baza = new List<baza>();

il_baza.Add(new baza());
il_baza.Add(new baza());
il_baza.Add(new baza());

//foliwing code works fine...it finds function "ElementAt"
il_baza.ElementAt(0);
il_baza.ElementAt(1);
il_baza.ElementAt(2);


Type type = (Type)il_baza.GetType();//returns type "List`1", why ?

object[] arg = new object[1];
arg[0] = 0;

//folowing code fires error message! it does not find "ElementAt"
type.InvokeMember("ElementAt", BindingFlags.Default |
BindingFlags.InvokeMethod, null, il_baza, arg);

}
}
 

Lesen sie die antworten

#1 Hilmar Bunjes
26/08/2009 - 17:40 | Warnen spam
Milan schrieb:

First: This is a German newsgroup/discussion group.

I have noticed that once IList object is created with:
IList<baza> il_baza = new List<baza>();

type of object "il_baza" is not any more "IList" but "List`1". Also I have
no clue what does it means "`1" in type definition, any idea ?



It's a generic class so it's not called "List" but "List`1". During
runtime the framework can tell you which type it is exactly so it
doesn't return "IList" but "List`1".

Later if I try with reflection to InvokeMember:
type.InvokeMember("ElementAt", BindingFlags.Default |
BindingFlags.InvokeMethod, null, il_baza, arg);

I get MissingMethodException error message because of type is not any more
IList:

Die Methode "System.Collections.Generic.List`1[[Reflection.baza,
ConsoleApplication2, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]].ElementAt" konnte nicht gefunden werden.



That's correct because ElementAt doesn't exist for IList<> or List<>.
That method is defined in System.Linq.Enumerable and is an extension for
IList<>.

HTH,
Hilmar

Ähnliche fragen