繁体   English   中英

如何将对象转换为键入c#

[英]How to cast object to type c#

我的类型变量很少

Int16[], Double[]

我把他们推到这样的功能

Int16[] variable = ...;
Func(variable);

要么

Double[] variable = ...;
Func(variable);

///////////////////////////////
private void Func(Object input)
{
    var data = (input.GetType())input; ?????

    //some actions with data
}

在这种情况下

var data = input.ToString();

数据变为包含内容“System.Int16 []”或“System.Double []”的字符串

如何在func中输入我的数据变为Int16 []或Double []类型的输入对象,即数据应该是Int16 []或Double []的数组,或者我可以执行的任何类型我可以执行的操作,例如此操作:

for(int i = 0; i < data.length; ++i)
{
    data[i] = data[i] * 5;
}

这里有几个选项,首先你可以使用通用函数:

private void MyFunction<T>(T[] values)
{
    for (int i = 0; i < values.Length; i++)
    {
        //Here is where the issue is, you can't constrain T to a value type that
        //defines mathematical operators, so the best you can do is dynamic:
        values[i] = (dynamic)values[i] * 5;
    }
}

或者你可以这样做:

private void MyFunction(object values)
{
    //Assume that object is an array, and go from there
    for (int i = 0; i < ((dynamic)values).Length; i++)
    {
        ((dynamic)values)[i] = ((dynamic)values)[i] * 5;
    }
}

我觉得哪个更脏。 无论哪种方式,您应该在这些函数的顶部进行某种类型检查,以验证传入的参数是否为数字数组类型,然后才假定它正在运行代码。

您发布的行:

var data = (input.GetType())input;

显然不起作用,因为GetType()返回一个System.Type ,它在编译时不会用类型的名称替换它自己,所以它不是一个有效的强制转换。

你可以Convert.ChangeType见下文:

var valueType = typeof (TValue);
valueType = Nullable.GetUnderlyingType(valueType) ??  valueType;

var value = (TValue) Convert.ChangeType(environmentValue, valueType);

我想你想要这样的Type参数:

private void Func<T>(T[] input)
    {
        var data = input; //Unnecessary but since you used it, so did I.
        // Now you can perform actions with data.
        // Call this function like this:
        // Func<Double>(variable) or Func<Int16>(variable) or any other type you want.
    }

如何在func中转换我的数据变为Int16 []或Double []类型的输入对象,

如果使用数组的操作占主导地位,我会将通用算术运算封装在接口中,以便从通用函数中使用它。

interface IArithmeticOperation<T> 
{
    void Shift(T[] left, object value);
    void Scale(T[] left, object value);
}

private void MyFunction<T>(T[] data) 
{   
    var ops = GetOperations<T>();
    ops.Scale(data, 5);
}


IArithmeticOperations<T> GetOperations<T>()
{ 
    object result;

    switch(Type.GetTypeCode(typeof(T))
    {
       case TypeCode.Double:
         result = new DoubleArithmeticOperations();  
         break;
       case TypeCode.Int16:
         result = new Int16ArithmeticOperations();  
         break;

       defaut:            
           throw new InvalidOperationException("Unsupported type");
    }

    return (IArithmeticOperation<T>) result;
}

用法:

var int16Data = new short[] {1, 2, 3, 4};
MyFunc(int16Data);

var doubleData = new double[] {1.0, 2.0, 3.0, 4.0};
MyFunc(doubleData);

可能的实施:

class DoubleArithmeticOperation: IArithmeticOperation<Double>
{
    public void Shift(double[] left, object value) 
    { 
       double v = Convert.ToDouble(value);
       for(int i = 0; i < left.Length, ++i} {left[i] += v};
    }

    public void Scale(double[] left, object value) 
    {
        double v = Convert.ToDouble(value);
        for(int i = 0; i < left.Length, ++i} {left[i] *= v};
    }
}


class Int16ArithmeticOperation: IArithmeticOperation<Int16>
{
    public void Shift(short[] left, object value) 
    { 
        short v = Convert.ToInt16(value); 
        for(int i = 0; i < left.Length, ++i} {left[i] += v};
    }

    public void Scale(short[] left, object value) 
    {
        short v = Convert.ToInt16(value); 
        for(int i = 0; i < left.Length, ++i} {left[i] *= v};
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM