7

私はできるようにしたい:

  • オブジェクトにインデックス演算子が定義されているかどうかを確認します。
  • 定義されているのであれば、それを使用できるようにしたいです。

これを次のコードで実装したいと思います。このコードには、多次元配列またはリンクされた一連のハッシュ テーブルを走査する方法を提供するオブジェクト (MyObject) が含まれています。また、要求されたパスにノードが存在しない場合にエラーが発生するのを防ぐ必要があります。私が理解できない部分は、コード内のコメント部分です。

public class MyObject
{
    private object myObject = null;

    public MyObject()
    {
    }

    public MyObject(object value)
    {
        myObject = value;
    }

    public void setValue(object value)
    {
        myObject = value;
    }

    public object getValue()
    {
        return myObject;
    }

    public object this[string key]
    {
        get
        {
            if (myObject == null) 
            {
                return new MyObject(null);
            }
            else
            {
                // determine what of type/class myObject is and if it has indexing operators defined
                // if defined, access them and return the result
                // else return null.
            }
        }
        set
        {
            if (myObject == null)
            {
                // do nothing (or throw an exception);
            }
            else{
                // determine what of type/class myObject is
                // determine if that type/class has indexing operators defined
                // if defined, access them and set the result there
                // else do nothing (or throw an exception).
            }
        }
    }
}

これが私が達成したいことです:

        // given these variables:
        string loremIpsumString = "lorem ipsum dolor sit amet";
        int[] digits = new int[10];
        for (int i = 0; i <= 3; i++) digits[i] = i;
        Hashtable outerHashtable = new Hashtable();
        Hashtable innerHashtable = new Hashtable();
        innerHashtable.Add("contents", "this is inside");
        outerHashtable.Add("outside", "this is outside");
        outerHashtable.Add("inside", innerHashtable);

        // I can already print this:
        Response.Write(    loremIpsumString    ); // prints "lorem ipsum dolor sit amet"
        Response.Write(    digits[0]    ); // prints "0"
        Response.Write(    digits[1]    ); // prints "1"
        Response.Write(    digits[2]    ); // prints "2"
        Response.Write(    outerHashtable["outside"]    ); // prints "this is outside"
        Response.Write(    ((Hashtable)outerHashtable["inside"])["contents"]    ); // prints "this is outside"

        // But I want to be to do it this way:
        MyObject myObject;

        myObject = new MyObject(loremIpsumString);
        Response.Write(    myObject.getValue()    ); // prints "lorem ipsum dolor sit amet"
        Response.Write(    myObject["unexistant"].getValue()    ); // prints nothing/null
        myObject = new MyObject(digits);
        Response.Write(    myObject[0].getValue()    ); // prints "0"
        Response.Write(    myObject[1].getValue()    ); // prints "1"
        Response.Write(    myObject[2].getValue()    ); // prints "2"
        myObject = new MyObject(outerHashtable);
        Response.Write(    myObject["outside"].getValue()    ); // prints "this is outside"
        Response.Write(    myObject["inside"]["contents"].getValue()    ); // prints "this is inside"
        Response.Write(    myObject["unexistant"].getValue()    ); // prints nothing/null
        Response.Write(    myObject["unexistant"]["unexistant"]["unexistant"].getValue()    ); // prints nothing/null
4

3 に答える 3

9

IListまず、カバー (ジェネリック)Listsと配列に継承されているかどうかを確認できます。そうでない場合PropertyInfo.GetIndexParametersは、代わりにインデクサーがあるかどうかを確認するために使用できます。

get
{
    if (myObject == null)
    {
        return null;
    }
    else
    {
        // not sure which index(es) you want
        int index = 0;
        Type t = myObject.GetType();
        if (typeof(IList).IsAssignableFrom(t))
        {
            IList ilist = (IList)myObject;
            return ilist[index];
        }
        else
        {
            var indexer = t.GetProperties()
                .Where(p => p.GetIndexParameters().Length != 0)
                .FirstOrDefault();
            if (indexer != null)
            {
                object[] indexArgs = { index };
                return indexer.GetValue(myObject, indexArgs);
            }
            else
                return null;
        }
    }
}

DEMO (文字にアクセスするためのインデクサーstringを持つ

于 2013-01-22T16:09:04.893 に答える
1

オブジェクトが辞書かどうかをテストできます

public object this[string key]
{
    get
    {
        var dict = myObject as IDictionary;
        if (dict == null) {
            return null;
        }
        if (dict.Contains(key)) {
            return dict[key];
        }
        return null;
    }
    set
    {
        var dict = myObject as IDictionary;
        if (dict != null) {
            dict[key] = value;
        }
    }
}

注: 使用する辞書タイプを制御できる場合はDictionary<string,object>Hashtable. その便利な方法TryGetValueにより、最初に呼び出すことなく安全にアクセスできるContainsため、2 回アクセスする必要がなくなります。Dictionary<string,object>当然のことながら、代わりに にキャストしますIDictionary

var dict = myObject as Dictionary<string,object>;
if (dict == null) {
    return null;
}
object result;
dict.TryGetValue(key, out result); // Automatically sets result to null
                                   // if an item with this key was not found.
return result;
于 2013-01-22T16:23:18.087 に答える
0

答えを探している他の人のために。これは、@TimSchmelter の助けを借りて作成したものです。

これは、この画面上部のコードで使用した get{} に実装したコードです。この画面上部の get{} にはコメントが含まれています。

get
{
    if (myObject == null)
        return new MyObject(null);
    object returnValue = null;
    bool foundReturnValue = false;
    object[] indexArgs = { key };
    Type myObjectType = myObject.GetType();
    if (typeof(IList).IsAssignableFrom(myObjectType))
    {
        try
        {
            returnValue = ((IList)myObject)[((int)key)];
            foundReturnValue = true;
        }
        catch (Exception) { }
    }
    if (!foundReturnValue)
    {
        foreach (PropertyInfo property in myObjectType.GetProperties())
        {
            ParameterInfo[] indexParameters = property.GetIndexParameters();
            foreach (ParameterInfo indexParameter in indexParameters)
            {
                if (indexParameter.ParameterType.IsAssignableFrom(key.GetType()))
                {
                    try
                    {
                        returnValue = property.GetValue(myObject, indexArgs);
                        foundReturnValue = true;
                    }
                    catch (Exception) { }
                }
                if (foundReturnValue == true)
                    break;
            }
            if (foundReturnValue == true)
                break;
        }
    }
    return new MyObject(returnValue);
}
于 2013-01-23T10:50:08.773 に答える