0

使用 C# 4.0。我想将 的实例转换MyBuffer<int>为 的实例MyBuffer<float>。转换器必须足够通用以处理其他基本类型。

我希望这个(或等效的解决方案)能够工作。如何?

var b1 = MyBuffer.Create(new int[100]);
var b2 = Convert.ChangeType(b1, typeof(MyBuffer<float>));
var b3 = Convert.ChangeType(b2, typeof(MyBuffer<byte>));

考虑MyBuffer类:

public class MyBuffer
{
    public static MyBuffer<T> Create<T>(T[] buffer)
        where T : struct, IComparable, IConvertible
    {
        return new MyBuffer<T>(buffer);
    }
}

public class MyBuffer<T> : IConvertible
    where T : struct, IComparable, IConvertible
{
    public T[] Buffer
    {
        get;
        set;
    }

    public MyBuffer()
    {
    }

    public MyBuffer(T[] buffer)
    {
        Buffer = buffer;
    }

    public object ToType(Type conversionType, IFormatProvider provider)
    {
        if (conversionType == GetType())
            return this;

        // First problem: Determine if the type is MyBuffer<>.
        // if (conversionType == typeof(MyBuffer<>))
        {
            if (conversionType.IsGenericType && conversionType.GetGenericArguments().Length > 0)
            {
                var bufferType = conversionType.GetGenericArguments()[0];
                dynamic newBuffer = Buffer.
                    Select(s => Convert.ChangeType(s, bufferType))
                    .ToArray();

                // Second problem: Our dynamic variable will produce an exception here.
                return MyBuffer.Create(newBuffer);
            }
        }

        throw new InvalidCastException();
    }

    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    //
    // For completeness...
    //

    public TypeCode GetTypeCode()
    {
        throw new NotImplementedException();
    }

    public bool ToBoolean(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public byte ToByte(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public char ToChar(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public DateTime ToDateTime(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public decimal ToDecimal(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public double ToDouble(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public short ToInt16(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public int ToInt32(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public long ToInt64(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public sbyte ToSByte(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public float ToSingle(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public string ToString(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public ushort ToUInt16(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public uint ToUInt32(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }

    public ulong ToUInt64(IFormatProvider provider)
    {
        throw new NotImplementedException();
    }
}

在我上面的代码中,有两个主要问题需要解决:

  1. 你如何确定一个Type变量是否等于SomeType<T>任意值T
  2. 是否可以调用T设置为某个Type变量的模板化函数?
4

3 回答 3

4

由于MyBuffer<T>非常有用地包含一个将缓冲区作为参数包装的构造函数,因此您可以简单地将一个缓冲区手动转换为另一个缓冲区(使用 LINQ 非常容易)并创建一个新的“转换后”实例:

var b1 = MyBuffer.Create(new int[100]);
var b2 = MyBuffer.Create(b1.Buffer.Select(i => (float)i).ToArray());

// another way to do the same:
var b3 = MyBuffer.Create(b1.Buffer.Select(Convert.ToSingle).ToArray());

更新:

为了减轻Daniel对我可能有隐藏意图的担忧,这里是如何通过反射来完成问题所要求的,但更方便的形式是运行时在您的位置进行挖掘:

dynamic ConvertArray<T>(T[] input, Type target) {
    var result = Array.CreateInstance(target, input.Length);
    for (var i = 0; i < input.Length; ++i)
    {
        result.SetValue(Convert.ChangeType(input[i], target), i);
    }

    return result;
}

此方法允许您执行此操作:

var ints = new[] { 1, 2, 3 };
var strings = ConvertArray(ints, typeof(string));
foreach (var s in strings) {
    Console.WriteLine("[{0}] {1}", s.GetType(), s + " potato");
}

很明显,它strings的行为与字符串数组完全一样。当然,存在dynamic意味着这个特定的数组永远无法与例如 lambdas 混合,并且反射的道德等价物仍然在运行时进行(只是你看不到它)。所以这不是免费的午餐,但有时它可以证明是有用的。

于 2013-05-02T12:24:45.367 回答
1

只是把这个扔出去。有一个很棒的库用于转换对象类型,称为 AutoMapper。

http://automapper.codeplex.com/

于 2013-05-02T22:28:50.023 回答
1

对于第一个问题:

if (conversionType.GetGenericTypeDefinition() == typeof(MyBuffer<>))

对于第二个问题:

//your method....
//....
if (conversionType.IsGenericType && conversionType.GetGenericArguments().Length > 0)
{ //in fact you don't need this if in case the first problem is solved.

    var bufferType = conversionType.GetGenericArguments()[0];
    Func<MyBuffer<object>> AuxMethod = BufferConversion<object>;
    return AuxMethod.Method.GetGenericMethodDefinition().MakeGenericMethod(bufferType).Invoke(this, null);            
 }
//....continue your method....


private MyBuffer<NewType> BufferConversion<NewType>()
{
    NewType[] MyNewArray = Buffer.Select(s => (NewType)Convert.ChangeType(s, typeof(NewType))).ToArray();
    return MyBuffer.Create(MyNewArray);
}
于 2013-05-02T20:23:40.457 回答