简体   繁体   English

有没有办法为此类/接口使用“友好”名称?

[英]Is there a way to use a 'friendy' name for this class/interface?

Basically I just think the type name is ugly when passing it around my project. 基本上,我只是认为类型名称在我的项目中传递时很难看。 I know you can't use an interface that implements the interface because then it's a new interface that just happens to implement the original interface, right? 我知道您不能使用实现该接口的接口,因为那是刚好实现原始接口的新接口,对吗?

public class GenericFactory<T, TTypeEnum> : IGenericFactory<T, TTypeEnum>
{
    private readonly IIndex<TTypeEnum, Func<CCompParms, T>> _factory;

    public GenericFactory(IIndex<TTypeEnum, Func<CCompParms, T>> factory)
    {
        _factory = factory;
    }

    public T Create(TTypeEnum serviceType, CCompParms comp)
    {
        return _factory[serviceType](comp);
    }
}

public interface IGenericFactory<T, TTypeEnum>
{
    T Create(TTypeEnum serviceType, CCompParms comp);
}

I have tried: 我努力了:

public interface FriendlyName : IGenericFactory<T, TTypeEnum>
{
}

But when I try to do the following it fails to cast no matter how I cast it. 但是,当我尝试执行以下操作时,无论我如何投射,它都无法投射。

IGenericFactory<T, TTypeEnum> inter = GetTheInterface();
FriendlyName fn = (inter as FriendlyName);

Is there a way to make the type name friendly? 有没有一种方法可以使类型名称友好?

First, any general solution for a "Friendly Name" will still have to be parameterized with both of the generic types, so I don't think that's what you're looking for since it won't really save you any typing. 首先,对于“友好名称”的任何常规解决方案仍必须使用这两种泛型类型进行参数化,因此,我认为这不是您要查找的内容,因为它实际上不会节省任何键入内容。

Assuming you want FriendlyName to already have the types bound, then I think you can get to a workable solution by using Implicit Conversions and the Decorator pattern. 假设您希望FriendlyName已经绑定了类型,那么我认为您可以通过使用隐式转换Decorator模式来获得可行的解决方案。

WARNING!!! 警告!!! I just typed this into the browser (no IDE or compiler) and my C# is very rusty, so this will likely need to be tweaked 我只是在浏览器中输入了此代码(没有IDE或编译器),而我的C#非常生锈,因此可能需要对其进行调整

public interface FooFactory : IGenericFactory<Foo, FooEnum> {

    IGenericFactory<Foo, FooEnum> Wrapped { get; }

    // The "magic" - Note that magic always makes your code harder to understand...

    public static implicit operator FooFactory(IGenericFactory<Foo, FooEnum> wrapped) {
        // I think this can be placed here. If C# won't let you add this
        // implicit operator here, then you can easily implement this factory
        // method as an extension on IGenericFactory<Foo, FooEnum>
        return new FooFactoryWrapper(wrapped);
    }

    public static implicit operator IGenericFactory<Foo, FooEnum>(FooFactory wrapper) {
        return wrapper.Wrapped;
    }

    // I'm pretty sure we can hide this implementation here in the interface,
    // but again, my C# is pretty rusty, so you may have to move this
    // and/or change the visibility
    private class FooFactoryWrapper : FooFactory {

        public IGenericFactory<Foo, FooEnum> Wrapped { get; private set; }

        public FooFactoryWrapper(IGenericFactory<Foo, FooEnum> wrapped) {
            this.wrapped = wrapped;
        }

        // Since the "friendly type" is still an instance of the base type,
        // you'll still have to fully implement that interface. Just delegate
        // all calls to your wrapped type (most useless Decorator ever)

        public Foo Make() { return Wrapped.Make(); } // sample method in IGenericFactory<>
    }
}

Now, you should be able to use it like this: 现在,您应该可以像这样使用它:

IGenericFactory<Foo, FooEnum> inter = GetTheInterface();
FooFactory fn = inter; // implicit conversion to wrapper type

DoWork(fn); // use the "friendly name" like it were it's wrapped type
            // implicit conversion back to wrapped type

public void DoWork(IGenericFactory<Foo, FooEnum> fooFactory) {
    ...
}

All that being said, I wouldn't go through this effort. 话虽这么说,我不会为此付出努力。 Whenever I've made "Friendly Name" types like this, I then make them part of my "model" and treat them as proper types, which means that I directly ask for them in method signatures and constructors. 每当我这样创建“友好名称”类型时,就将它们作为“模型”的一部分并将其视为适当的类型,这意味着我直接在方法签名和构造函数中要求它们。

Something like this: 像这样:

public interface BarFactory : IGenericFactory<Bar, BarEnum> { }

// Asking for a BarFactory and not a IGenericFactory<Bar, BarEnum>
public void DoWork(BarFactory barFactory) { ... }

Much less typing and no need for magic. 打字少得多,不需要魔术。

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

相关问题 通过接口使用静态类? - Use static class by interface? 获取实现接口的类的名称 - Get name of class that implements an interface 问题与解答-如何以强类型方式获取属性/函数/操作/方法(接口或类的名称)? - Q&A - How to get the name of Property / Function / Action / Method (of an Interface or Class), in a strongly type way? 引用没有类名称的接口名称 - Reference an interface name without the class name 有没有一种方法可以通过将某个类/接口/…用其名称空间而不是使用指令“ using namespace_name”封闭起来来引用它? - Is there a way to reference a certain class/interface/… by enclosing it with its namespace rather than a using directive “using namespace_name”? 有没有办法使用具有相同名称但在派生类中具有不同类型的属性? - Is there a way to use a property with same name but is of different type in derived class? 强制 class 的实例使用接口 - Force an instance of class to use an interface 如何使用与singleton类的接口 - how to use interface with singleton class 有什么方法可以使用类扩展方法来支持C#中的接口方法吗? - Any way to use a class extension method to support an interface method in C#? 解析IoC接口以隐式命名为具有相同名称的类 - Resolve IoC interface to class with same name implicitily
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM