代码之家  ›  专栏  ›  技术社区  ›  Rudiger W.

C#扩展方法中可以有init once变量吗?

  •  0
  • Rudiger W.  · 技术社区  · 11 月前

    扩展方法对类的实例进行操作,但它们是作为静态类中的静态方法实现的。他们似乎不允许声明任何变量 static readonly . 这里有一个人为的例子来说明这个问题。我如何对其进行编码,以便 scammers 只初始化一次?

    sealed class Caller // can't be modified
    {
        public long Phone { get; set; }
    }
    
    static class Validator
    {
        public static Caller Validate(this Caller caller)
        {
            // I want to initialize this only once from a more elaborate source
            HashSet<long> scammers = new() { 2345678901, 3456789012, 4567890123 };
    
            if (scammers.Contains(caller.Phone)) throw new ArgumentException("known scammer");
            return caller;
        }
    }
    

    编辑: 我可以看到,移动的宣言 骗子 out方法解决了init once问题。我之所以没有想到,是因为我 Validate(...) 方法实际上是通用的 Validate<T>(...) 并且变量需要具有相同类型的泛型。 看起来我需要将Validator类拆分,并将泛型方法放在一个单独的类中,该类本身就是T类型的泛型。这不起作用,因为扩展方法需要在非泛型静态类中。。。

    1 回复  |  直到 11 月前
        1
  •  0
  •   ipodtouch0218    11 月前

    我想我在你的问题中遗漏了一些东西。。。因为扩展类 支持静态变量。

    这工作得很好(在.NET 4.7.2和.NET 8中测试过,它不是旧版本的东西…):

    sealed class Caller // can't be modified
    {
        public long Phone { get; set; }
    }
    
    static class Validator
    {
        private static HashSet<long> scammers = new HashSet<long>() { 2345678901, 3456789012, 4567890123 };
        
        public static Caller Validate(this Caller caller)
        {
            if (scammers.Contains(caller.Phone)) throw new ArgumentException("known scammer");
            return caller;
        }
    }