Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将TSource值设置为字符串_C#_.net_Type Conversion - Fatal编程技术网

C# 将TSource值设置为字符串

C# 将TSource值设置为字符串,c#,.net,type-conversion,C#,.net,Type Conversion,我需要操作TSource变量 //代码: private static DataTable ToDataTable<TSource>(this IList<TSource> data) { foreach (TSource item in data) { switch (item.ToString()) { case

我需要操作
TSource
变量

//代码:

 private static DataTable ToDataTable<TSource>(this IList<TSource> data)
  {
          foreach (TSource item in data)
            {
                switch (item.ToString())
                 {
                    case "Name":
                        item = "John";  //Error here
                        break;
                 }
            }
  }

有什么建议吗?

由于TSource是泛型类型,您无法确保到
字符串的转换有效,因此
item=“John”始终显示编译错误

我认为你有两种可能:

  • 您可以假设集合不是
    TSource
    类型,并设置为
    IList
  • 您可以定义可以从字符串显式分配的基类型
例如:

internal class StringConvertible
{
    public static implicit operator string(StringConvertible value)
    {
        return value.StringValue;
    }

    public static implicit operator StringConvertible(string value)
    {
        return new StringConvertible
        {
            StringValue = value
        };
    }

    public virtual string StringValue { get; set; }
}

// ...

private static DataTable ToDataTable<TSource>(this IList<TSource> data)
{
    for (int index = 0; index < data.Count; index++)
    {
        if (!typeof(TSource).IsAssignableFrom(typeof(StringConvertible)))
        {
            continue;
        }

        StringConvertible value = data[index] as StringConvertible;
        switch (value)
        {
            case "Name":
                value.StringValue = "John";
                break;
        }     
    }

    // ...
}
内部类
{
公共静态隐式运算符字符串(字符串值)
{
返回值.StringValue;
}
公共静态隐式运算符StringConvertible(字符串值)
{
返回新的可转换字符串
{
StringValue=值
};
}
公共虚拟字符串StringValue{get;set;}
}
// ...
私有静态数据表ToDataTable(此IList数据)
{
for(int index=0;index
由于TSource是泛型类型,您无法确保到
字符串的转换有效,因此
item=“John”始终显示编译错误

我认为你有两种可能:

  • 您可以假设集合不是
    TSource
    类型,并设置为
    IList
  • 您可以定义可以从字符串显式分配的基类型
例如:

internal class StringConvertible
{
    public static implicit operator string(StringConvertible value)
    {
        return value.StringValue;
    }

    public static implicit operator StringConvertible(string value)
    {
        return new StringConvertible
        {
            StringValue = value
        };
    }

    public virtual string StringValue { get; set; }
}

// ...

private static DataTable ToDataTable<TSource>(this IList<TSource> data)
{
    for (int index = 0; index < data.Count; index++)
    {
        if (!typeof(TSource).IsAssignableFrom(typeof(StringConvertible)))
        {
            continue;
        }

        StringConvertible value = data[index] as StringConvertible;
        switch (value)
        {
            case "Name":
                value.StringValue = "John";
                break;
        }     
    }

    // ...
}
内部类
{
公共静态隐式运算符字符串(字符串值)
{
返回值.StringValue;
}
公共静态隐式运算符StringConvertible(字符串值)
{
返回新的可转换字符串
{
StringValue=值
};
}
公共虚拟字符串StringValue{get;set;}
}
// ...
私有静态数据表ToDataTable(此IList数据)
{
for(int index=0;index
除了HuorsWord指出的选项外,另一个选项可能是将自定义转换函数传递到ToDataTable,如下所示:

private static DataTable ToDataTable<TSource>(this IList<TSource> data, Func<string, TSource> itemFromString)
  {
          foreach (TSource item in data)
            {
                switch (item.ToString())
                 {
                    case "Name":
                        item = itemFromString("John");
                        break;
                 }
            }
  }
私有静态数据表到数据表(此IList数据,Func itemFromString)
{
foreach(数据中的源项)
{
开关(item.ToString())
{
案例“名称”:
item=itemFromString(“John”);
打破
}
}
}

除了HuorsWord指出的选项外,另一个选项可能是将自定义转换函数传递到ToDataTable,如下所示:

private static DataTable ToDataTable<TSource>(this IList<TSource> data, Func<string, TSource> itemFromString)
  {
          foreach (TSource item in data)
            {
                switch (item.ToString())
                 {
                    case "Name":
                        item = itemFromString("John");
                        break;
                 }
            }
  }
私有静态数据表到数据表(此IList数据,Func itemFromString)
{
foreach(数据中的源项)
{
开关(item.ToString())
{
案例“名称”:
item=itemFromString(“John”);
打破
}
}
}

此代码将如何编译?无法分配给项,因为它在ForEach中。为什么要将项设置为字符串?如果您想将项用作字符串,为什么要使用泛型类?这段代码将如何编译?无法分配给项,因为它在ForEach中。为什么要将项设置为字符串?如果要将项用作字符串,为什么要使用泛型类?至少有两个错误:
StringConvertible value=(StringConvertible)(object)项您需要对对象进行中间转换,或者必须将
StringConvertible value=item用作StringConvertible
。第二个更重要的问题是,您不能
返回新的StringConvertible(value)
,因为
StringConvertible
是一个抽象类,然后您真的想创建一个
TSource
.Ops!真的。。。示例已删除!更好的例子是使用虚拟抽象getter/setter(公共抽象字符串StringValue{get;set;})。修改正在枚举的项必须“小心”执行。至少有两个错误:
stringconverable value=(stringconverable)(object)项您需要对对象进行中间转换,或者必须将
StringConvertible value=item用作StringConvertible
。第二个更重要的问题是,您不能
返回新的StringConvertible(value)
,因为
StringConvertible
是一个抽象类,然后您真的想创建一个
TSource
.Ops!真的。。。示例已删除!更好的例子是使用虚拟抽象getter/setter(公共抽象字符串StringValue{get;set;})。修改正在枚举的项必须“小心”进行