有没有办法限制 C# class 中的函数只使用特定的签名?

Is there a way to restrict functions in a C# class to only use a specific signature?

我正在写一个 class,理想情况下应该有多个相同签名的方法。如果所有方法都遵循相同的签名,是否有办法强制 class 检查其方法?

如果检查可以在 compile-time/during build

完成就更理想了

如果您假设签名是 int <methodName>(string, int, char)

public class Conditions {
        // no error
        int MethodA(string a, int b, char c)
        {
            return 0;
        }
        // no error
        int MethodB(string a, int b, char c)
        {
            return 1;
        }

        // should throw error because return type does not match signature
        string MethodC(string a, int b, char c)
        {
            return "Should throw an error for this function";
        }
    }
}

不直接。您可以使用 Roslyn 为它编写一个分析器,或者您可以编写一个单元测试来通过反射检查签名。

您可以进行单元测试:

    [TestMethod]
    public void Conditions_MethodsHaveCorrectSignature()
    {
        var whitelist = new List<string> { "Finalize", "MemberwiseClone" };
        var t = typeof(Conditions);
        var m = t.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);

        foreach (var item in m.Where(x => !whitelist.Contains(x.Name)))
        {
            Assert.AreEqual(typeof(int), item.ReturnType);

            CollectionAssert.AreEquivalent(new List<Type> { typeof(string), typeof(int), typeof(char) },
                item.GetParameters().Select(x => x.ParameterType).ToList());
        }
    }

有点作弊,但如果您要求开发人员注册他们的方法,您可以通过要求方法匹配委托来强制编译时错误。

这基本上就是事件处理程序和回调的工作方式。

namespace Framework
{
    public delegate int MyApiSignature(int a, string b, char c);

    public class Core
    {
        static public void RegisterMethod(MyApiSignature method) 
        {
            //Doesn't even have to actually do anything
        }
    }
}


namespace Custom
{
    using Framework;

    class Foo
    {
        public Foo()
        {
            Core.RegisterMethod(MethodA);  //Works
            Core.RegisterMethod(MethodB);  //Compile-time error
        }

        public int MethodA(int a, string b, char c)
        {
            return 0;
        }

        public int MethodB(int a, string b, byte c)
        {
            return 0;
        }
    }
}