如何在通用设置中使用类型(=静态)成员

发布于 2025-01-17 08:57:57 字数 941 浏览 3 评论 0原文

我想就静态方法/属性签订合同,以便在通用设置中使用它们。像这样:

interface IAnimal {
    static string Sound;
}

class Dog : IAnimal {
    static string Sound => "woof";
}

class Cat : IAnimal {
    static string Sound => "meow";
}

class AnimalSoundExplainer<T> where T : IAnimal {

    // This does not work (but I would like it to):
    internal static void Explain() =>
        Console.WriteLine("Every " + typeof(T).Name + " makes " + T.Sound);
}

我会这样使用它:

AnimalSoundExplainer<Dog>.Explain(); // should write "Every Dog makes woof"
AnimalSoundExplainer<Cat>.Explain(); // should write "Every Cat makes meow"
  1. 我如何制定该合同(这样,如果我不履行合同,我会收到编译错误)? C# 的静态接口成员不是这样工作的; C# 将始终仅使用(提供或未提供)IAnimal 的实现。它确实允许实现/覆盖类似于非静态成员的静态成员。

  2. 如何在通用设置中使用该约定,即如何从给定的通用类型参数访问这些成员

  • 而不需要生成实例,
  • 也不使用使我的程序变慢的反射方法? (如果有反射方法不会使我的程序变慢,我会同意它们。)

I want to make a contract about static methods/properties in order to use them in a generic setting. Like this:

interface IAnimal {
    static string Sound;
}

class Dog : IAnimal {
    static string Sound => "woof";
}

class Cat : IAnimal {
    static string Sound => "meow";
}

class AnimalSoundExplainer<T> where T : IAnimal {

    // This does not work (but I would like it to):
    internal static void Explain() =>
        Console.WriteLine("Every " + typeof(T).Name + " makes " + T.Sound);
}

I would use it like this:

AnimalSoundExplainer<Dog>.Explain(); // should write "Every Dog makes woof"
AnimalSoundExplainer<Cat>.Explain(); // should write "Every Cat makes meow"
  1. How can I make that contract (so that I get compile errors if I do not fulfill the contract)? C#'s static interface members do not work that way; C# will always just use the (provided or not) implementation of IAnimal. It does allow implementing/overriding static members analog to non-static members.

  2. How can I make use of that contract inside a generic setting, i.e. how can I access theses members from a given generic type argument

  • without needing to generate instances and
  • without using reflection methods that make my program slow?
    (If there are reflection methods that do not make my program slow, I'd be okay with them.)

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

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

发布评论

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

评论(1

不寐倦长更 2025-01-24 08:57:57

此功能称为“ “,它是当前正在预览中.net 6

If you're happy enabling preview functionality, the following works in .NET 6 preview:

interface IAnimal {
    static abstract string Sound { get; }
}

class Dog : IAnimal {
    public static string Sound => "woof";
}

class Cat : IAnimal {
    public static string Sound => "meow";
}

class AnimalSoundExplainer<T> where T : IAnimal {

    internal static void Explain() =>
        Console.WriteLine("Every " + typeof(T).Name + " makes " + T.Sound);
}

See它在sharplab上

This functionality is called "static abstract members", and it is currently in preview in .NET 6.

If you're happy enabling preview functionality, the following works in .NET 6 preview:

interface IAnimal {
    static abstract string Sound { get; }
}

class Dog : IAnimal {
    public static string Sound => "woof";
}

class Cat : IAnimal {
    public static string Sound => "meow";
}

class AnimalSoundExplainer<T> where T : IAnimal {

    internal static void Explain() =>
        Console.WriteLine("Every " + typeof(T).Name + " makes " + T.Sound);
}

See it on SharpLab.

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