首页 > 解决方案 > C# 为 ICollection 或 IEnumerable 中的特定项目创建 getter 委托

问题描述

我正在尝试创建一个动态Environment类来托管模拟的实时数据。我希望能够注册特定的“环境变量”,例如集合、字段等。使用它,消费类将能够查看可用的变量并单独请求它们。

我想让这个基于反射的,以便任何未来的开发人员都可以采用现有的类并将其合并到其中,Environment而无需实现其他功能。如果可能,我想添加对ICollection和/或IEnumerable接口的支持,以便可以使用实现这些的现有类。例如,能够注册 aDictionary意味着环境会将所有键值对列为环境变量,其中键被转换为唯一的字符串,值是根据请求提供的值。

一个如何实现它的例子:

public class Environment
{
  private delegate object GetterDelegate();

  private Dictionary<string, GetterDelegate> environmentVariables_;

  public IEnumerable<string> EnvironmentVariables
  {
    get => environmentVariables_.Keys;
  }

  public object this[string name]
  {
    get => environmentVariables_[name]();
  }

  public Environment()
  {
    environmentVariables_ = new Dictionary<string, GetterDelegate>();
  } 

  public void Register( string name, ICollection collection )
  {
    int i = 0;
    foreach( var element in collection )
      environmentVariables_.Add( $"name_{i++}", GetterDelegate );
  }

  public void Register( string name, IEnumerable enumerable )
  {
    int i = 0;
    foreach( var element in enumerable )
      environmentVariables_.Add( $"name_{i++}", GetterDelegate );
  }

  public void Register<T,V>( string name, Dictionary<T,V> dictionary )
  {
    // TODO: Custom logic instead of Key.ToString()
    foreach( var pair in dictionary )
      environmentVariables_.Add( $"name_{pair.Key.ToString()}", GetterDelegate );
  }

  public void Register( string name, FieldInfo field )
  {
    environmentVariables_.Add( name, GetterDelegate );
  }

}

为了实现这一点,我希望能够动态编译可以直接访问特定元素的 getter 方法,而不必IEnumerable.ElementAt()每次都调用,因为这可能会非常慢,具体取决于类的实现。而且由于ICollectionimplements IEnumerable,在大多数情况下它可能会以相同的方式处理。

是否可以编译一个可以直接获取特定 IEnumerable 元素而无需调用 ElementAt() 的 DynamicMethod,这可能会枚举整个集合,直到找到合适的元素?如果这太迂回了,我也欢迎使用更好的方法来解决这个问题。

标签: c#delegatessimulationienumerable

解决方案


IEnumerable如果ICollection您需要能够按索引访问项目,请不要使用。这些接口都不支持。

IList是表示可以通过索引访问的数据的接口。


推荐阅读