例如我有两个结构体struct A
{
 public int Field1;
 public int Field2;
}struct B
{
 public int Field1;
 public int Field2;
}
通过重载操作符号,可以得到 
B b=new B();
A  a=(A)b;但是却不能
A[] arr=(A[])barr;
。我又想通过一个方法去转换
   
        /// <summary>
        ///把原始数组转换为目标数组
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="source">原始数组</param>
        /// <returns>成功返回转换后的数组</returns>
 public static T[] Transfer<T>(object source) where T : new()
        {
            if (!(source is Array))
                return null;            IList enumerAbleSource = (IList)source;
            List<T> l = new List<T>();
            try
            {
                foreach (var item in enumerAbleSource)
                {                    
                    l.Add((T)(item));
                }
            }
            catch
            {
                return null;
            }            return l.ToArray();
        }但是这个方法是不行的,
跪求大侠们一个方法,可以转换转换两个不同结构体的数组,不要foreach挨个转换,因为用的地方多,看起来很重复

解决方案 »

  1.   

    在 .net  4以前时无法完成的
    A[] a = new A[];
    B[] b = new b[];
    foreach(A f in a)
     b <- f
      

  2.   

    - -用 linq 的 select 还是可以比较简单的完成
      config.MainVideoEncOpt = wrapConfig.stMainVideoEncOpt.Select(arg => (VideoEncOpt)arg).ToArray();
      

  3.   

    exp:            string[] str = new string[] { "1", "2" };
                int[] ints;
                ints = Array.ConvertAll(str, new Converter<string, int>(delegate(string s)
                {
                    return Convert.ToInt32(s);
                }));
                MessageBox.Show(ints.Length.ToString());