我将一个数组作为Web方法中的对象传递.如何在Web方法中将对象转换为数组
public static string sample(object arr)
{
string[] res= (string[])arr; //convertion object to string array
return "";
}
Run Code Online (Sandbox Code Playgroud)
我得到这样的错误
Flair.dll中出现"System.InvalidCastException"类型的异常,但未在用户代码中处理
考虑到您必须将mylist类型转换List<T>为List<Base>where T的子类Base
这些解决方案是否相同?哪个表现更好,为什么?我什么时候应该更喜欢使用第一个或第二个?
return mylist.Cast<Base>().ToList();
return mylist.ConvertAll(x => (Base)x);
Run Code Online (Sandbox Code Playgroud)
也许第二个解决方案可能会更好,因为mylist是直接转换的。
在第一个解决方案中,列表转换为IEnumerable,然后转换为列表,但我不确定。