Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 索引器的扩展方法,好吗?_C#_Methods_Data Access Layer_Indexer - Fatal编程技术网

C# 索引器的扩展方法,好吗?

C# 索引器的扩展方法,好吗?,c#,methods,data-access-layer,indexer,C#,Methods,Data Access Layer,Indexer,索引器的扩展方法,好吗 我在玩一些重新加密POCO的代码 代码循环从SqlDataReader返回的行,并使用反射从列值分配属性。在我的调用堆栈中,我有这样一行代码:- poco.Set("Surname", "Smith"); // uses extension method ... Set方法是作为扩展方法编写的 如果能够编写这样的代码那就太好了 poco["Surname"] = "Smith"; // extension methods for indexers ? ie我想为in

索引器的扩展方法,好吗

我在玩一些重新加密POCO的代码

代码循环从SqlDataReader返回的行,并使用反射从列值分配属性。在我的调用堆栈中,我有这样一行代码:-

poco.Set("Surname", "Smith"); // uses extension method ...
Set方法是作为扩展方法编写的

如果能够编写这样的代码那就太好了

poco["Surname"] = "Smith";  // extension methods for indexers ?
ie我想为indexer编写一个扩展方法

是否有充分的理由说明.Net没有索引器的扩展方法? 其他人对扩展方法索引器有其他好的用途吗

作为旁白。。。 如果我们可以为索引器编写扩展方法,那么我们可以编写如下代码

var poco = PocoFactory();  
    poco.Surname = “Smith”; // is this JavaScript ...
    poco[Surname] = “Smith” ; // … or is this c# or both
我的代码中的一些片段

/////////////////////////////////////////////
// Client calling code
IDab dab = DabFactory.Create( "Northwind" );
string sql = @"select * from Customers ";
var persons = dab.ExecuteReader<NorthwindCustomer>(sql);
if (dab != null{
   Assert.That(persons[0].CustomerID , Is.EqualTo("ALFKI"));}
/////////////////////////////////////////////
List<T> IDab.ExecuteReader<T>(string commandText) 
{
    List<T> pocos = new List<T>();
    // setup connection
    SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
    while (reader.Read())
    {
            Dictionary<string, int> colMappings = null ;
            if (colMappings == null){
                colMappings = reader.GetSqlDataReaderColumnMappings();}
            T poco = new T();
            poco.DbToMem<T>(reader, colMappings);
            pocos.Add(poco);
        }
    }
    // connection cleanup ...
    return pocos ;
}

// the set extension method signature
public static void Set<T>(this T thisClientObject, string thisPropertyName, object newValue) where T : class
/////////////////////////////////////////////
//客户端调用代码
IDab dab=DabFactory.Create(“北风”);
字符串sql=@“从客户中选择*”;
var persons=dab.ExecuteReader(sql);
如果(dab!=null{
Assert.That(persons[0].CustomerID,Is.EqualTo(“ALFKI”);}
/////////////////////////////////////////////
列表IDab.ExecuteReader(字符串commandText)
{
List pocos=新列表();
//设置连接
SqlDataReader=command.ExecuteReader(CommandBehavior.CloseConnection);
while(reader.Read())
{
字典colMappings=null;
if(colMappings==null){
colMappings=reader.GetSqlDataReaderColumnMappings();}
T poco=新的T();
poco.DbToMem(阅读器,colMappings);
poco.Add(poco);
}
}
//连接清理。。。
返回POCO;
}
//集扩展签名方法
公共静态无效集(此T thisClientObject、字符串thisPropertyName、对象newValue),其中T:class

索引器与属性有很多共同之处(在后台,索引器是一个带有索引的属性),扩展属性不存在。同意,在某些情况下它们很方便

在某些方面,这与
dynamic
非常相似。当然,如果您声明了一个具有字符串索引器的接口,那么您的读者代码可以直接使用它-但是重复实现此接口将是大量不必要的工作

Re是扩展方法;这是否使用常规反射?您可能想看看这样的技巧,如果您大量使用这种方法,可能会节省大量CPU时间。典型的用法是:

PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
while (reader.Read())
{
     T poco = new T();
     // abbreviated...
     (per prop)
        props[propName].SetValue(poco, cellValue);
}
您可以通过首先查看返回的列(每个网格一次,而不是每行一次)并仅访问匹配的列来进一步优化此操作


或者,看看ORM工具;
Expression
也可以用来读取数据(我在usenet上的某个地方有一个完整的例子,比如DbLinq)

对于Pocos补水,我建议您看看AutoMapper Nuget软件包。它非常简单,大大减少了代码量。

非常有趣,谢谢,我会看一看。