简体   繁体   English

将接口类型作为参数传递

[英]Pass Interface Type As Parameter

I already know that you can pass an interface as a parameter to a method. 我已经知道您可以将接口作为参数传递给方法。 This allows you to specify only the relevant members of an object required by a method. 这允许您仅指定方法所需的对象的相关成员。 What I would like to do is to be able to pass an interface type as a parameter. 我想要做的是能够将接口类型作为参数传递。

Say I declared several interfaces, which were implemented un-evenly across a range of objects that all form a single list/collection. 假设我声明了几个接口,它们在一系列对象中不均匀地实现,这些对象都形成一个列表/集合。 Could I write a helper method which would take both an object from the list and an interface type as a parameter, and check if the object implements the interface? 我可以编写一个辅助方法,它既可以从列表中获取对象,也可以将接口类型作为参数,并检查对象是否实现了接口? The following code is obviously rubbish, but it illustrates the sort of thing I want to do: 以下代码显然是垃圾,但它说明了我想要做的事情:

private bool CheckType(object o, interface intrfce)
{
    try
    {
        object x = (object)(intrfce)o;
        return true;
    }
    catch (InvalidCastException e) 
    {
        return false
    }
}

At the moment I'm simply planning on setting up an enum for the interfaces, and requiring all classes to expose an array/list of interfaces they implement. 目前我只是计划为接口设置枚举,并要求所有类公开它们实现的接口的数组/列表。 I can then just check the enum list to see what interfaces they have that are relevant (I'm only interested in the interfaces I have created - I'm not after returning IEnumerable or ICloneable etc.) Or I could write helper methods for each interface. 然后我可以检查枚举列表以查看它们具有哪些相关的接口(我只对我创建的接口感兴趣 - 我不是在返回IEnumerableICloneable等之后)或者我可以为每个接口编写辅助方法接口。 I was just wondering if there was a more elegant way of doing it? 我只是想知道是否有一种更优雅的方式呢?

You can do it using generics: 你可以使用泛型来做到这一点:

private bool CheckType<T>(object o) {
    return o is T;
}

You call it like this: 你这样称呼它:

foreach (object o in myList) {
    if (CheckType<MyInterface>(o)) {
        ... // Do something
    }
}

Considering how easy it is to do, you might as well do it in the conditional itself. 考虑到它是多么容易,你可以在条件本身中做到这一点。

Finally, if you wish to process only objects implementing a particular interface in a mixed list, you could do it with LINQ's OfType method, like this: 最后,如果您希望仅处理在混合列表中实现特定接口的对象,则可以使用LINQ的OfType方法来执行此操作,如下所示:

foreach (MyInterface o in myList.OfType<MyInterface>()) {
   ...
}

You can do something like: 你可以这样做:

private bool CheckType(object o, params Type[] types)
{
    //you can optionally check, that types are interfaces
    //and throw exceptions if non-interface type was passed
    if(types.Any(type => !type.IsInterface))
        throw new Exception("Expected types to have only interface definitions");

    return types.All(type => type.IsAssignableFrom(o.GetType()));
}


CheckType(new List<int>(), typeof(IEnumerable), typeof(IList)); //returns true
CheckType(0, typeof(IEnumerable)); //return false

To check a sequence of objects, you can use something along: 要检查一系列对象,您可以使用以下内容:

private bool CheckAllType(IEnumerable<object> items, params Type[] types)
{
    return items.All(item => CheckType(item, types));
}

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

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