Validate if field is part of an interface

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

    Validate if field is part of an interface

    Hi,

    Is't possible to find out if a field is declared in a specific
    interface using Reflection?

    -pseudo code-
    Type type = MyObject.GetTyp e();
    fieldInfo = type.GetFields( ...)
    Debug.writeLine (IsDeclaredInIn terface(fieldIn fo, IMyInterface))

    -J E E
  • Aravind C

    #2
    Re: Validate if field is part of an interface

    Hi Joneks,

    You can enumerate through the fields defined in your interface
    and check against the field type you are comparing.

    Type typeCompare = MyObject.GetTyp e();

    FieldInfo[] fields = typeof(IMyInter face).GetFields ();
    foreach(FieldIn fo field in fields)
    {
    if(field.FieldT ype == typeCompare)
    {
    // Match found
    break;
    }
    }

    Regards,
    Aravind C


    "Warpig" <joneks@hotmail .com> wrote in message
    news:ce5a61fb.0 311100353.297ef 096@posting.goo gle.com...[color=blue]
    > Hi,
    >
    > Is't possible to find out if a field is declared in a specific
    > interface using Reflection?
    >
    > -pseudo code-
    > Type type = MyObject.GetTyp e();
    > fieldInfo = type.GetFields( ...)
    > Debug.writeLine (IsDeclaredInIn terface(fieldIn fo, IMyInterface))
    >
    > -J E E[/color]


    Comment

    • Mattias Sjögren

      #3
      Re: Validate if field is part of an interface

      [color=blue]
      >Is't possible to find out if a field is declared in a specific
      >interface using Reflection?[/color]

      C# interfaces can't contain field declarations, so that should never
      happen.



      Mattias

      --
      Mattias Sjögren [MVP] mattias @ mvps.org

      Please reply only to the newsgroup.

      Comment

      • Aravind C

        #4
        Re: Validate if field is part of an interface

        Mattias is right.
        Got carried away in my previous posts with the reflection methods without
        realising that fields cannot be a part of an interface.

        Regards,
        Aravind


        "Warpig" <joneks@hotmail .com> wrote in message
        news:ce5a61fb.0 311100353.297ef 096@posting.goo gle.com...[color=blue]
        > Hi,
        >
        > Is't possible to find out if a field is declared in a specific
        > interface using Reflection?
        >
        > -pseudo code-
        > Type type = MyObject.GetTyp e();
        > fieldInfo = type.GetFields( ...)
        > Debug.writeLine (IsDeclaredInIn terface(fieldIn fo, IMyInterface))
        >
        > -J E E[/color]


        Comment

        Working...