C# - 更换for.NET ArrayList.ToArray(类型)在Silverlight [英] C# - Replacement for.NET ArrayList.ToArray(Type) in Silverlight

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

问题描述

下面是一个简单的方法,我写的(极度简化下来,我希望它仍然得到了JIST对面),采取的字符串再以字符串数组的元素presentation,并将它们转换为那些实际阵列值。 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;
}

然后,我会用这种方法与下面的code。的PropertyInfo是一个对象的属性,在这种情况.IsArray()==真。 sArrayElements只是包含字符串重数组的presentation(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对象被标记为内部或东西(不能使用类型,因为访问级别等等等等等等)。

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(类型)。我不能只用一个List&LT;对象&gt; .ToArray(),因为PropertyInfo.SetValue()调用将在努力使一个Object []成一个Int32 []或bawk类似

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的四,任何帮助将不胜AP preciated:)

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# - 更换for.NET ArrayList.ToArray(类型)在Silverlight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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