简体   繁体   English

获取实现接口的类的名称

[英]Get name of class that implements an interface

I have some entities, that may or may not inherit from other objects, but they will implement an interface, lets call it IMyInterface. 我有一些实体,可能会也可能不会从其他对象继承,但是它们将实现一个接口,将其称为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. 如果我传入MyClass,它应该返回MyClass,因为MyClass实现了接口。

If i pass in MyHighClass, it should return MyClass, because MyClass was inherited, and it implements the interface. 如果我传入MyHighClass,它应该返回MyClass,因为MyClass是继承的,并且它实现了接口。

If i pass in PlainInheritedClass, it should return PlainInheriedClass, because it inherited from MyPlainClass, but that did not implement the interface, PlainInheritedClass did 如果我传入PlainInheritedClass,则它应该返回PlainInheriedClass,因为它继承自MyPlainClass,但是没有实现该接口,PlainInheritedClass做到了

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. 我正在使用实体框架6。我创建了一种回收站功能,该功能允许用户删除数据库中的数据,但实际上它只是将其隐藏。 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. 但是,当我“重置”属性时,我正在使用自己的SQL语句。 In order to create my own SQL statements, i need to find out which table has the column i need to update. 为了创建自己的SQL语句,我需要找出哪个表具有需要更新的列。

I cannot use entity framework to load the entities based on the type only, because .Where doesnt exist on a generic DbSet class. 我不能使用实体框架仅基于类型加载实体,因为通用DbSet类上不存在.Where

So i want to create an SQL statement similar to this 所以我想创建一个与此类似的SQL语句

UPDATE tableX SET interfaceProperty = NULL WHERE interfaceProperty = X

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM