1

I have some entities, that may or may not inherit from other objects, but they will implement an interface, lets call it IMyInterface.

public interface IMyInterface {
    long MyPropertyName { get; set; }
}

An object will always implement this interface, but it may have been implemented on a class that the object inherits from. How can i get the name of the class that has this interface implemented?

Examples should give these results

public class MyClass : IMyInterface {

}

public class MyHighClass : MyClass {

}

public class MyPlainClass {

}

public class PlainInheritedClass : MyPlainClass, IMyInterface {

}

If i pass in MyClass, it should return MyClass, because MyClass implements the interface.

If i pass in MyHighClass, it should return MyClass, because MyClass was inherited, and it implements the interface.

If i pass in PlainInheritedClass, it should return PlainInheriedClass, because it inherited from MyPlainClass, but that did not implement the interface, PlainInheritedClass did

EDIT/ EXPLAINATION

I am working with entity framework 6. I have created a sort of recycle bin feature, that allows users to delete data on the database, but really it just hides it. In order to use this feature, an entity must implement an interface, which has a particular property against it.

Most of my entities do not inherit from anything, but just implement the interface. But i have a couple of entities that do inherit from another object. Sometimes the object they are inheriting from implement the interface and sometimes the object itself will implement the interface.

When i set the value, i use the entities and entity framework works out which table to update. But when i "unset" the property, i am using my own SQL statements. In order to create my own SQL statements, i need to find out which table has the column i need to update.

I cannot use entity framework to load the entities based on the type only, because .Where doesnt exist on a generic DbSet class.

So i want to create an SQL statement similar to this

UPDATE tableX SET interfaceProperty = NULL WHERE interfaceProperty = X
Gillardo
  • 9,518
  • 18
  • 73
  • 141

1 Answers1

0

I was just over thinking the whole thing, the function was very easy. Just encase someone needs something siliar, here it is, i have made it generic. You could always make it an extension instead.

Code just interates all the way down, to the base class, and then checks each class on the way back up through the tree.

public Type GetImplementingClass(Type type, Type interfaceType)
{
    Type baseType = null;

    // if type has a BaseType, then check base first
    if (type.BaseType != null)
        baseType = GetImplementingClass(type.BaseType, interfaceType);

    // if type
    if (baseType == null)
    {
        if (interfaceType.IsAssignableFrom(type))
            return type;
    }

    return baseType;
}

So i had to call this like so, with my examples

// result = MyClass
var result = GetClassInterface(typeof(MyClass), typeof(IMyInterface));

// result = MyClass
var result = GetClassInterface(typeof(MyHighClass), typeof(IMyInterface));

// result = PlainInheritedClass 
var result = GetClassInterface(typeof(PlainInheritedClass), typeof(IMyInterface));
Gillardo
  • 9,518
  • 18
  • 73
  • 141