我希望能够:
我想在以下代码中实现它。 该代码包含一个对象(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
答案 0 :(得分:8)
您可以先检查它是否继承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;
}
}
}
答案 1 :(得分: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
,从而使您无法访问它两次。因此,您会转而使用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;
答案 2 :(得分: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);
}