C# - Silverlight 中 .NET ArrayList.ToArray(Type) 的替换 [英] C# - Replacement for.NET ArrayList.ToArray(Type) in Silverlight

查看:21
本文介绍了C# - Silverlight 中 .NET ArrayList.ToArray(Type) 的替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我写的一个简单的方法(极度简化,所以我希望它仍然可以理解),用于获取字符串中数组元素的字符串表示,并将它们转换为这些值的实际数组.t 是数组的类型.

Below is a simple method I wrote (extremely simplified down so I hope it still gets the jist across) for taking a string representation of an array's elements in a string, and converting them into an actual array of those values. t is the type of the array.

DeserializeArray(string sArrayElements, out Array aValues, Type t)
{
    string[] sValues = ProcessArrayElements(sArrayAsString);
    ArrayList alValues = new ArrayList(sValues.Length);
    for(int i = 0; i < sValues.Length; ++i)
        alValues.Add(ProcessValue(sValues[ i ] ));
    aValues = alValues.ToArray(t.GetElementType());
    return true;
}

然后我会在下面的代码中使用这个方法.propertyInfo 是在这种情况下 .IsArray() == true 的对象的属性.sArrayElements 只是包含数组的字符串表示的字符串 ("val1,val2,...,valN")

I would then use this method with the code below. propertyInfo is the property of an object that in this case .IsArray() == true. sArrayElements is just the string that contains the string representation of the array ("val1,val2,...,valN")

Array aValues;
if (DeserializeArray(sArrayElements, out aValues, propertyInfo.PropertyType))
    propertyInfo.SetValue(oObject, aValues, null);
else
    throw new FormatException("Unable to parse Array Elements: " + sArrayElements);

这在 .NET 中工作得很好,但在 Silverlight 中却不是,因为 ArrayList 对象被标记为 Internal 或其他(不能使用类型,因为访问级别等等).

This works beautifully in .NET, but not in Silverlight because the ArrayList object is marked as Internal or something (can not use type because access level blah blah blah).

所以我正在寻找 ArrayList.ToArray(Type) 的替代方案.我不能只使用 List<object>.ToArray() 因为 PropertyInfo.SetValue() 调用会在尝试将 object[] 变成 Int32[] 或类似的东西时犹豫不决.

So I am looking for an alternative to the ArrayList.ToArray(Type). I can not just use a List<object>.ToArray() because the PropertyInfo.SetValue() call will bawk at trying to make an object[] into an Int32[] or the like.

我尝试在 DeserializeArray() 方法中执行类似 aValues = Array.CreateInstance(t.GetElementType()) 的操作,但我无法使用 [] 来分配值,您也无法将值分配给 foreach(obj在对象中).

I have tried in the DeserializeArray() method to do something like aValues = Array.CreateInstance(t.GetElementType()) but I can not use [] to assign the values and you can not assign values to the foreach(obj in objects).

然后,我尝试将 aValues 参数更改为通用对象 [] 数组,但在运行时调用 Array.CreateInstance() 时出现相同的转换(装箱/拆箱)错误.

I then tried changing the aValues parameter to a generic object[] array but i get the same conversion (boxing/unboxing) errors at run time when calling the Array.CreateInstance().

所以是的;我正在尝试为 Silverlight 4 找到此问题的解决方案.任何帮助将不胜感激:)

So yeah; I am trying to find a solution to this problem for Silverlight 4. Any help would be greatly appreciated :)

  • 詹姆斯

推荐答案

未测试,但我认为这应该可以满足您的要求:

Not tested, but I think this should do what you want:

DeserializeArray(string sArrayElements, out Array aValues, Type t) 
{ 
    string[] sValues = ProcessArrayElements(sArrayAsString); 
    aValues = new Array[sValues.Length];
    for(int i = 0; i < sValues.Length; ++i) 
        aValues.SetValue(Activator.CreateInstance(t,ProcessValue(sValues[i])),i); 

    return true; 
}

这篇关于C# - Silverlight 中 .NET ArrayList.ToArray(Type) 的替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆