C#:需要方法参数的索引器

时间:2021-01-15 19:49:34

I'm writing a function which is a mapper from one type of object to another. The object which is passed in as a parameter of the Map method should have a string indexer to find the appropriate value in the object (or not). I don't really care if the mapper gets a Dictionary, DataRow, DataReader, etc.

我正在编写一个函数,它是从一种对象到另一种对象的映射器。作为Map方法的参数传入的对象应该有一个字符串索引器,以在对象中找到适当的值(或不是)。我真的不在乎mapper是否有Dictionary,DataRow,DataReader等。

Is there a way to specificy that the parameter passed to the method should implement a string indexer? I can't find anything like it.

有没有办法具体说明传递给方法的参数应该实现字符串索引器?我找不到类似的东西。

I'll use reflection to cast the type to something usable if it's not possible, but I was wondering if there is proper way to handle this.

如果不可能,我将使用反射将类型转换为可用的类型,但我想知道是否有正确的方法来处理它。

4 个解决方案

#1


No, you can't do that. You could use an interface like Andrew's, or you could pass in a Func<T, string> that retrieves an indexed value.

不,你做不到。您可以使用像Andrew这样的界面,或者您可以传入一个Func 来检索索引值。 ,string>

public void ConsumeIndexedFunction<T>(Func<string, T> something)
{
    var foo = something("bar");
    // do something with foo
}

public void TestMethod(
    Dictionary<string, object> myDictionary,
    DataTable myDataTable,
    IDataReader myDataReader)
{
    ConsumeIndexedFunction(x => myDictionary[x]);
    ConsumeIndexedFunction(x => myDataTable.Rows[0][x]);
    ConsumeIndexedFunction(x => myDataReader[x]);
}

(Credit to Michael Meadows for fleshing out this idea into an example. Thanks!)

(感谢Michael Meadows将这个想法充实成一个例子。谢谢!)

#2


You can create an interface like this:

您可以创建如下界面:

interface IFoo
{
    String this[Int32 index] { get; set; }
}

All types that implement this interface will have to implement a string indexer and if the type of the parameter to your method is IFoo you can know that it will have an indexer.

实现此接口的所有类型都必须实现字符串索引器,如果方法的参数类型是IFoo,则可以知道它将具有索引器。

#3


Unless you can make all objects (or classes respectively) you like to pass to the mapper implement an interface which defines such an indexer, you will need to use reflection.

除非您可以将所有对象(或类别)分别传递给映射器实现定义此类索引器的接口,否则您将需要使用反射。

#4


Would an Interface not help in this case:

在这种情况下,接口是否会有所帮助:

interface IImplementMe {
    string this[int index]
    {
        get;
        set;
    }
}

Just require that the parameter implements the Interface

只需要参数实现接口即可

#1


No, you can't do that. You could use an interface like Andrew's, or you could pass in a Func<T, string> that retrieves an indexed value.

不,你做不到。您可以使用像Andrew这样的界面,或者您可以传入一个Func 来检索索引值。 ,string>

public void ConsumeIndexedFunction<T>(Func<string, T> something)
{
    var foo = something("bar");
    // do something with foo
}

public void TestMethod(
    Dictionary<string, object> myDictionary,
    DataTable myDataTable,
    IDataReader myDataReader)
{
    ConsumeIndexedFunction(x => myDictionary[x]);
    ConsumeIndexedFunction(x => myDataTable.Rows[0][x]);
    ConsumeIndexedFunction(x => myDataReader[x]);
}

(Credit to Michael Meadows for fleshing out this idea into an example. Thanks!)

(感谢Michael Meadows将这个想法充实成一个例子。谢谢!)

#2


You can create an interface like this:

您可以创建如下界面:

interface IFoo
{
    String this[Int32 index] { get; set; }
}

All types that implement this interface will have to implement a string indexer and if the type of the parameter to your method is IFoo you can know that it will have an indexer.

实现此接口的所有类型都必须实现字符串索引器,如果方法的参数类型是IFoo,则可以知道它将具有索引器。

#3


Unless you can make all objects (or classes respectively) you like to pass to the mapper implement an interface which defines such an indexer, you will need to use reflection.

除非您可以将所有对象(或类别)分别传递给映射器实现定义此类索引器的接口,否则您将需要使用反射。

#4


Would an Interface not help in this case:

在这种情况下,接口是否会有所帮助:

interface IImplementMe {
    string this[int index]
    {
        get;
        set;
    }
}

Just require that the parameter implements the Interface

只需要参数实现接口即可