简体   繁体   English

如何在C#中将params object [i]传递给模板函数

[英]How to pass params object[i] to a template function in C#

I am trying to send a params object[i] to a T function but I cant find the right syntax. 我正在尝试将params object [i]发送到T函数,但是找不到正确的语法。

Here is an example to show the context of what I am trying to achieve: 这是一个示例,显示我要实现的目标的上下文:

private bool decodeValue<T>(int id,ref T item, string code)
{

    if(!TypeDescriptor.GetConverter (item).CanConvertFrom(typeof(string)))
    {
        errorThrow (id + 2);
        return false;
    }

    try
    {
        item = ((T)TypeDescriptor.GetConverter (item).ConvertFromString (code));
    }
    catch 
    {
        errorThrow (id + 2);
        //item = default(T);
        return false;
    }

    return true;
}

private bool decodeValues(string[] code, int id, params object[] items)
{
    for (int i = 0; i < items.Length; i++) 
    {
        System.Type t = items [i].GetType(); 
        //in this part i cant find the correct syntax
        if(decodeValue<(t)items[i]>(id, ref items[i] as t, code[i+2]))
        {

        }
    }

    return false;
}

At the line decodeValue<(t)items[i]>(id, ref items[i] as t, code[i+2] no matter what syntax I try, the compiler tells me that after > it expects a ) 处的decodeValue<(t)items[i]>(id, ref items[i] as t, code[i+2]无论我尝试使用哪种语法,编译器都告诉我在>之后需要a )

I could inline the function decodeValue into the for loop but I think there is a more elegant way of doing it. 我可以将函数decodeValue内联到for循环中,但是我认为有一种更优雅的方法。 Any suggestions? 有什么建议么?

In your example, you replace content of items. 在您的示例中,您替换了项目的内容。 Why do even need templated function? 为什么甚至需要模板化功能? Just do this: 只要这样做:

private bool decodeValue(int id,ref object item, string code)
{

    if(!TypeDescriptor.GetConverter(item).CanConvertFrom(typeof(string)))
    {
        errorThrow (id + 2);
        return false;
    }
    try
    {
        item = TypeDescriptor.GetConverter(item).ConvertFromString(code);
    }
    catch 
    {
        errorThrow (id + 2);
        return false;
    }

    return true;
}

private bool decodeValues(string[] code, int id, params object[] items)
{
    for (int i = 0; i < items.Length; i++) 
    {
        //in this part i cant find the correct syntax
        if(decodeValue(id, ref items[i], code[i+2]))
        {

        }
    }

    return false;
}

If you ever need type of item in your code, just call .GetType() where it actualy needed. 如果您需要代码中的项目类型,只需在实际需要的地方调用.GetType()。 That is not only good way to do things, but in some cases performance effective (compiler can greatly optimise some calls of this function). 这不仅是做事的好方法,而且在某些情况下可以提高性能(编译器可以极大地优化此函数的某些调用)。

generics ( <T> ) and reflection ( .GetType() ) are not good friends. 泛型( <T> )和反射( .GetType() )不是好朋友。 There is no convenient way to express that. 没有方便的表达方式。 You can , however, abuse dynamic to do it, but you can't easily use ref in that case. 可以 ,但是,滥用dynamic做到这一点, 你不能轻易使用ref在这种情况下。 Further, you can't ref between an object and a T . 此外,您不能在objectT之间进行ref So basically, there's a lot of hurdles. 所以基本上,有很多障碍。 Frankly, I think you should assess whether decodeValue<T> really needs to be generic. 坦白说,我认为您应该评估一下是否确实需要使用decodeValue<T> In this scenario - especially when using TypeDescriptor - I very much doubt that it needs to be. 在这种情况下-特别是在使用TypeDescriptor -我非常怀疑它是否需要。 I suspect your best bet would be to have: 我怀疑您最好的选择是:

private bool DecodeValue(int id, ref object item, Type type, string code)

The generics' syntax if for compile time while GetType() is for runtime. 如果GetType()用于运行时,则泛型语法用于编译时。 You cannot mix them of use them together. 您不能将它们混合使用。

A generic class is resolved while you source is converted to binary. 将源转换为二进制时,将解析通用类。 The type or interface provided in a generic type is used in that area. 该区域使用通用类型提供的类型或接口。

On the contrary GetType returns a type during execution. 相反,GetType在执行期间返回一个类型。 That type cannot be inserted in a generic definition because compilation has already be done at that time. 不能在通用定义中插入该类型,因为那时已经完成了编译。

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

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