Ran*_*dom 9 .net c# indexing properties
我目前正在尝试在我的类定义中实现"索引"属性.
例如,我有以下课程:
public class TestClass
{
private int[] ids = null;
public string Name { get; set; }
public string Description { get; set; }
public int[] Ids {
get
{
//Do some magic and return an array of ints
//(count = 5 - in this example in real its not fixed)
return _ids;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我喜欢使用这个类如下:
private void DoSomething()
{
var testClass = GetSomeTestClass();
//work with the ids
for (int i = 0; i < 10; i++) //I know I could say i < Ids.Length, its just an example
{
int? id = testClass.Ids[i];
//this will result, in a out of bound exception when i reaches 5 but I wish for it to return a null like a "safe" index call ?!?
}
}
Run Code Online (Sandbox Code Playgroud)
那么是否有一个安全的索引调用导致null,而不需要我在try catch中再次包装它.
另一件事我不想使用类索引,因为我需要几个像这样工作的属性,具有不同的类型(int,string,bool,custom class等).
(再一次,for只是一个简单的例子,我知道在这种情况下我可以说"i <Ids.Length")
如果您只对已经不可为空的类型数据感兴趣,例如,struct您可以使用简单的扩展方法
public static class ArrayExt
{
public static Nullable<T> GetValueOrNull(this T[] array, int index) where T: struct
{
return array.Length < index ? new Nullable<T>(array[index]) : null;
}
}
Run Code Online (Sandbox Code Playgroud)
这会让你简单地打电话
int? id = testClass.Ids.GetValueOrNull(i);
Run Code Online (Sandbox Code Playgroud)
但是,鉴于您需要支持任意数量的类型,我的建议是实现数组的包装并控制您访问数据的方式,例如
public class SafeArray<T>
{
private T[] items;
public SafeArray(int capacity)
{
items = new T[capacity];
}
public object this[int index]
{
get
{
return index < items.Length ? (object)items[index] : null;
}
set
{
items[index] = (T)value;
}
}
}
public class TestClass
{
public TestClass()
{
Ids = new SafeArray<int>(5);
Instances = new SafeArray<MyClass>(5);
}
...
public SafeArray<int> Ids { get; private set; }
public SafeArray<MyClass> Instances { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
这种方法的关键是object用作返回类型.这允许您将数据(或盒子/ unbox,如果使用值类型)转换为接收端的预期类型,例如
for (int i = 0; i < 10; i++)
{
// we need an explicit cast to un-box value types
var id = (int?)testClass.Ids[i];
// any class is already of type object so we don't need a cast
// however, if we want to cast to original type we can use explicit variable declarations e.g.
MyClass instance = testClass.Instances[i];
}
Run Code Online (Sandbox Code Playgroud)
好的,全新的方法。由于您有几种可能的类型并且想要一个“小丑”方法,因此您可以将这些值存储为类中的键/值集合,然后这种方法就成为可能。
首先,在内部存储值:
public class TestClass
{
private Dictionary<Type, Array> _values = new Dictionary<Type, Array>();
}
Run Code Online (Sandbox Code Playgroud)
现在用实际数据填充该集合:
_values.Add(typeof(int?), new int[] { 1, 2, 3 });
_values.Add(typeof(string), new string[] { "a", "b", "c", "d", "e" });
Run Code Online (Sandbox Code Playgroud)
最后是小丑方法:
public T Get<T>(int index)
{
Type type = typeof(T);
Array array;
if (_values.TryGetValue(type, out array))
{
if (index >= 0 && index < array.Length)
{
return (T)array.GetValue(index);
}
}
return default(T);
}
Run Code Online (Sandbox Code Playgroud)
用法:
for (int i = 0; i < 10; i++)
{
int? id = testClass.Get<int?>(i);
string name = testClass.Get<string>(i);
//...
}
Run Code Online (Sandbox Code Playgroud)