代码生成器来生成内联抽象类来履行 C# 接口的 java 匿名内部类的作用?

发布于 2024-10-01 11:31:48 字数 821 浏览 2 评论 0原文

注意:我知道 C# 有匿名类型和内部类型,但它们所扮演的角色与旧的 java 匿名内部类明显不同。

假设我有一个接口:

public interface Foo<T>
{
    int Fizz(string a);
    T Buzz(object o);
    void Crackle();
}

是否有一些东西(ReSharper 宏等)允许我生成一个实现类,该类将履行 java 风格 AIC 的角色(即允许我定义内联功能)。即

public class FooAIC<T> : Foo<T>
{
    public Func<string, int> Fizz;
    public Func<object, T> Buzz;
    public Action Crackle;

    int Foo<T>.Fizz(string a)
    {
        return Fizz(a);
    }

    T Foo<T>.Buzz(object o)
    {
        return Buzz(o);
    }

    void Foo<T>.Crackle()
    {
        Crackle();
    }
}

(我没有详细考虑这一点。所以我想可以创建一个更智能的类,具有只读成员注入、调用时的空检查、适用于抽象类等,但是这个应该传达我想要实现的一般想法

有人知道可以做到这一点的东西吗?或者,除此之外,推荐最无痛的方法来考虑做这样的事情 - 这在 T4 引擎中可能吗?

Note: I'm aware that C# has anonymous types and inner types, but these fulfill markedly different roles from the java anonymous inner classes of old.

Suppose I have an interface:

public interface Foo<T>
{
    int Fizz(string a);
    T Buzz(object o);
    void Crackle();
}

Is there something out there (ReSharper macro or the like) that would allow me to generate an implementing class that would fulfill the role of a java style AIC (ie, allow me to define functionality inline). i.e.

public class FooAIC<T> : Foo<T>
{
    public Func<string, int> Fizz;
    public Func<object, T> Buzz;
    public Action Crackle;

    int Foo<T>.Fizz(string a)
    {
        return Fizz(a);
    }

    T Foo<T>.Buzz(object o)
    {
        return Buzz(o);
    }

    void Foo<T>.Crackle()
    {
        Crackle();
    }
}

(I haven't thought this through in a great amount of detail. So I guess one could create a smarter class with readonly member injection, null checking on invocation, works for abstract classes etc, but this should convey the general idea of what I want to achieve)

Anybody know something which can do this? Or, barring that, recommend the most painfree way to look at doing something like this - would this be possible in the T4 engine?

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

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

发布评论

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

评论(1

墨落成白 2024-10-08 11:31:48

你的解决方案是好的,通常在.NET中你不需要匿名类,因为大多数时候你只需要重写/实现它们的方法,为此你有委托(我认为这是一个更好的选择)。但是,如果您需要重写/实现多个方法,那么为每个方法创建一个带有委托的存根类是最好的方法。在 WPF/Silverlight 中,ICommand 就是以这种方式实现的,您有一个 DelegateCommand 类,它接收一个 Func 来实现 bool CanExecute(对象参数),并接收一个 Action 来实现 Execute(对象参数)。在代码生成中最正确的方法是使用 T4 模板,创建一个模板并在其中添加一个参数(例如一个字符串,指示要为其创建 Delegateclass 的类的类名,然后使用反射来生成代码)。

希望我有帮助

Your solution is ok, tipically in .NET you don't need anonymous classes as most part of the time you only override/implement a method on them, and for that you have delegates (wich i think are a much better choice). But if you need to override/implement several methods, creating a stub class with delegates for each method is the best way. In WPF/Silverlight ICommand is implemented that way, you have a DelegateCommand class wich receives a Func to implement bool CanExecute(object parameter) and an Action to implement Execute(object parameter). The most correct way to do it in code generation is with T4 templates, create a template and a parameter in it (for example a string indicating the class name of the class you want to create a Delegateclass for, and use reflection to produce the code).

Hope i helped

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文