代码之家  ›  专栏  ›  技术社区  ›  Shantanu Gupta

如何在C中创建参数化属性#

  •  4
  • Shantanu Gupta  · 技术社区  · 15 年前

    如何在C_中创建参数化属性。

    public readonly string ConnectionString(string ConnectionName)
    {
        get { return System.Configuration.ConfigurationManager.ConnectionStrings[ConnectionName].ToString(); }
    }
    
    2 回复  |  直到 15 年前
        1
  •  14
  •   Gishu    15 年前

    在C中可以创建的参数化属性的唯一类型是 indexer property :

    public class MyConnectionStrings
    {
        private string GetConnectionString(string connectionName) { ... }
    
        public string this[string connectionName]
        {
            get { return GetConnectionString(connectionName); }
        }
    }
    

    否则,只需创建一个方法-这似乎更接近你正在寻找的。

        2
  •  0
  •   Matthew Flaschen    15 年前

    C 4 allows this ,但仅用于访问外部COM属性。