strange compile error for a simple example bug??

T

Tony Johansson

Hello!

I have a very simple example below. The problem is that I get the following
compile error
Error 1 Cannot implicitly convert type 'System.Reflection.MemberInfo[]' to
'MemberInfo[]' F:\C#\ConsoleApplication12\ConsoleApplication12\MemberInfo.cs
27 46 ConsoleApplication12

I can't understand why I get this compile error because according to the
documentation it should work.
Is this a bug perhaps ??

class MemberInfo
{
int MYVALUE;

public void THIS_IS_A_METHOD()
{}

public int myValue
{
set { MYVALUE = value; }
}

static void Main(string[] args)
{
string testclass = "MemberInfo";
Console.WriteLine("\nFöljande är medlemsinfo för klassen:{0}",
testclass);

Type MyType = Type.GetType(testclass);

MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
}
}

//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
I have a very simple example below. The problem is that I get the following
compile error
Error 1 Cannot implicitly convert type 'System.Reflection.MemberInfo[]' to
'MemberInfo[]' F:\C#\ConsoleApplication12\ConsoleApplication12\MemberInfo.cs
27 46 ConsoleApplication12

I can't understand why I get this compile error because according to the
documentation it should work.
Is this a bug perhaps ??

Well it's a bug, but in your code. You've declared a class called
"MemberInfo". That means whenever you refer to the name "MemberInfo" in
your code, it will mean *your* class.

Now Type.GetMembers returns an array with elements
System.Reflection.MemberInfo, which is unrelated to your MemberInfo
class - so you can't cast from one to the other.

I strongly suggest you change the name of your class to avoid it
conflicting with types in the framework.
 

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