strange compile error for a simple example bug??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    strange compile error for a simple example bug??

    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.Reflect ion.MemberInfo[]' to
    'MemberInfo[]' F:\C#\ConsoleAp plication12\Con soleApplication 12\MemberInfo.c s
    27 46 ConsoleApplicat ion12

    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_METHO D()
    {}

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

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

    Type MyType = Type.GetType(te stclass);

    MemberInfo[] Mymemberinfoarr ay = MyType.GetMembe rs();
    }
    }

    //Tony


  • cfps.Christian

    #2
    Re: strange compile error for a simple example bug??

    Well you named your class memberInfo.

    It's saying System.Reflecti on.MemberInfo does not cast into
    MyNamespace.Mem berInfo.

    MemberInfo[] Mymemberinfoarr ay = MyType.GetMembe rs();

    should be

    System.Reflecti on.MemberInfo[] Mymemberinfoarr ay =
    MyType.GetMembe rs();

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: strange compile error for a simple example bug??

      Tony Johansson <johansson.ande rsson@telia.com wrote:
      I have a very simple example below. The problem is that I get the following
      compile error
      Error 1 Cannot implicitly convert type 'System.Reflect ion.MemberInfo[]' to
      'MemberInfo[]' F:\C#\ConsoleAp plication12\Con soleApplication 12\MemberInfo.c s
      27 46 ConsoleApplicat ion12
      >
      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.Reflecti on.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.

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      Working...