通过委托生成接口实现的工具?

发布于 2024-08-01 14:33:53 字数 379 浏览 4 评论 0原文

我经常需要通过将实现委托给类的成员来实现接口。 这项任务非常繁琐,因为即使 Visual Studio 为接口方法生成存根,我仍然必须编写代码来委托实现。 它不需要太多思考,所以它可能可以通过代码生成工具自动化...

我可能不是第一个想到这一点的人,所以肯定已经有这样的工具了,但我找不到谷歌上有什么...有什么想法吗?


编辑:似乎 ReSharper 可以做到这一点,但它相当昂贵...是否有具有相同功能的免费替代品?

I often need to implement an interface by delegating the implementation to a member of my class. This task is quite tedious because, even though Visual Studio generates stubs for the interface methods, I still have to write the code to delegate the implementation. It doesn't require much thinking, so it could probably be automated by a code generation tool...

I'm probably not the first one to think of this, so there must be such a tool already, but I couldn't find anything on Google... Any idea ?


EDIT : it seems that ReSharper can do it, but it's pretty expensive... is there a free alternative with the same feature ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山田美奈子 2024-08-08 14:33:53

...请参阅动态代理...

但是...

将接口实现委托给实例变量或类型[如 typeof(type)] 的能力是 C# 和 VB.Net 非常需要的语言功能。 由于缺乏多重继承以及密封和非虚拟方法和属性的盛行,所需的无用代码量令人望而生畏。

我认为以下语言增强会起作用......
//C#

interface IY_1
{
  int Y1;
  int Y2;
}
...
..
interface IY_n
{
....
..
}


class Y : IY_1, IY_2, ....,IY_n
{
  private readonly Oy_1 Oy_1 = new Oy_1() supports IY_1, IY_2,...etc;   // <<-----
  private readonly Oy_2 Oy_2 = new Oy_2() supports IY_3, IY_8,...etc;
  public int Y2 {...}
}

“supports”关键字(或等价物,例如“defaults”)将使用接口的正常名称和签名映射语义来标识类字段值的有序列表,这些字段值“协助”实现一个或多个接口姓名和签名对应。 任何本地实现都具有优先权,并且单个接口也可以由多个字段值实现。

... see dynamic proxy...

However...

The ability to delegate interface implementation to an instance variable or type [as in typeof(type)], is a greatly needed language feature of C# and VB.Net. With the absence of multiple inheritance and the prevalence of sealed and non-virtual methods and properties, the amount of fluff code required is daunting.

I would think the following language enhancement would work...
//C#

interface IY_1
{
  int Y1;
  int Y2;
}
...
..
interface IY_n
{
....
..
}


class Y : IY_1, IY_2, ....,IY_n
{
  private readonly Oy_1 Oy_1 = new Oy_1() supports IY_1, IY_2,...etc;   // <<-----
  private readonly Oy_2 Oy_2 = new Oy_2() supports IY_3, IY_8,...etc;
  public int Y2 {...}
}

The "supports" keyword (or equivalent such as "defaults") would identify an ordered list of class field values that "assist" in the implementation of one or more interfaces, using the normal name and signature mappings semantics of interface of name and signature correspondence. Any local implementation would have precedence and, as well, a single interface may be implemented by multiple field values.

像极了他 2024-08-08 14:33:53

我已经使用 Resharper 几个月了,它有一个很棒的功能可以做到这一点。

例如,编写以下代码:

class MyList<T> : IList<T>
{
    private readonly IList<T> _list;
}

将脱字符号放在 _list 上,按 Alt + Ins(生成代码的快捷方式),然后选择“委派”成员”。 选择您需要的成员,R# 会为他们生成委派成员:

    public void Add(T item)
    {
        _list.Add(item);
    }

    public void Clear()
    {
        _list.Clear();
    }

    public bool Contains(T item)
    {
        return _list.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        _list.CopyTo(array, arrayIndex);
    }

    public bool Remove(T item)
    {
        return _list.Remove(item);
    }

    public int Count
    {
        get { return _list.Count; }
    }

    public bool IsReadOnly
    {
        get { return _list.IsReadOnly; }
    }

    public int IndexOf(T item)
    {
        return _list.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        _list.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        _list.RemoveAt(index);
    }

    public T this[int index]
    {
        get { return _list[index]; }
        set { _list[index] = value; }
    }

I've been using Resharper for a few months now, and it has a great feature to do this.

For instance, write the following code:

class MyList<T> : IList<T>
{
    private readonly IList<T> _list;
}

Place the caret on _list, press Alt + Ins (shortcut for Generate Code), and select "Delegating members". Select the members you need, and R# generates delegating members for them:

    public void Add(T item)
    {
        _list.Add(item);
    }

    public void Clear()
    {
        _list.Clear();
    }

    public bool Contains(T item)
    {
        return _list.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        _list.CopyTo(array, arrayIndex);
    }

    public bool Remove(T item)
    {
        return _list.Remove(item);
    }

    public int Count
    {
        get { return _list.Count; }
    }

    public bool IsReadOnly
    {
        get { return _list.IsReadOnly; }
    }

    public int IndexOf(T item)
    {
        return _list.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        _list.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        _list.RemoveAt(index);
    }

    public T this[int index]
    {
        get { return _list[index]; }
        set { _list[index] = value; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文