Validate if field is part of an interface

  • Thread starter Thread starter Warpig
  • Start date Start date
W

Warpig

Hi,

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

-pseudo code-
Type type = MyObject.GetType();
fieldInfo = type.GetFields(...)
Debug.writeLine(IsDeclaredInInterface(fieldInfo, IMyInterface))

-J E E
 
Hi Joneks,

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

Type typeCompare = MyObject.GetType();

FieldInfo[] fields = typeof(IMyInterface).GetFields();
foreach(FieldInfo field in fields)
{
if(field.FieldType == typeCompare)
{
// Match found
break;
}
}

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

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



Mattias
 
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
 
Back
Top