c# - 从 T 和字符串的隐式转换

标签 c# .net implicit-conversion

我目前有一个通用类,它允许使用表达式作为值。

public class Expression<T>
{
    public T Value { get; set; }
    public string ExpressionText { get; set; }

    public static implicit operator Expression<T>(string input)
    {
        if (string.IsNullOrEmpty(input))
            return null;

        if (input.StartsWith("="))
            return new Expression<T> { ExpressionText = input };

        var converter = TypeDescriptor.GetConverter(typeof(T));
        T value = (T)converter.ConvertFromString(input);

        return new Expression<T> { Value = value };
    }

    public static implicit operator Expression<T>(T value)
    {
        if (value == null)
            return null;

        return new Expression<T> { Value = value };
    }

我希望能够使用来自 T 和字符串的隐式转换来设置属性。但是,如果表达式是字符串类型,编译器无法决定使用哪种转换。

有解决这个问题的巧妙方法吗?

谢谢,

肖恩

最佳答案

您将无法保留两个隐式运算符并期望使用 Expression<string>因为 better conversion rule

Given an implicit conversion C1 that converts from a type S to a type T1, and an implicit conversion C2 that converts from a type S to a type T2, the better conversion of the two conversions is determined as follows:

  • If T1 and T2 are the same type, neither conversion is better.
  • If S is T1, C1 is the better conversion.
  • If S is T2, C2 is the better conversion.
  • If an implicit conversion from T1 to T2 exists, and no implicit conversion from T2 to T1 exists, C1 is the better conversion.
  • If an implicit conversion from T2 to T1 exists, and no implicit conversion from T1 to T2 exists, C2 is the better conversion.
  • If T1 is sbyte and T2 is byte, ushort, uint, or ulong, C1 is the better conversion.
  • If T2 is sbyte and T1 is byte, ushort, uint, or ulong, C2 is the better conversion.
  • If T1 is short and T2 is ushort, uint, or ulong, C1 is the better conversion.
  • If T2 is short and T1 is ushort, uint, or ulong, C2 is the better conversion.
  • If T1 is int and T2 is uint, or ulong, C1 is the better conversion.
  • If T2 is int and T1 is uint, or ulong, C2 is the better conversion.
  • If T1 is long and T2 is ulong, C1 is the better conversion.
  • If T2 is long and T1 is ulong, C2 is the better conversion.
  • Otherwise, neither conversion is better.

If an implicit conversion C1 is defined by these rules to be a better conversion than an implicit conversion C2, then it is also the case that C2 is a worse conversion than C1.

带有 Expression<string>你显然是第一种情况,编译器不会为你选择,而是 just stop there .

If there is not exactly one function member that is better than all other function members, then the function member invocation is ambiguous and a compile-time error occurs.

因此您将无法仅处理 with with 转换运算符;要么创建其他方法来显式处理字符串,在运算符之外将字符串转换为 T,要么创建一个包装抽象,它能够传输 T、T 的字符串表示或 ExpressionText 值并从中转换。

关于c# - 从 T 和字符串的隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26378247/

相关文章:

c# - Process.Start,如果输入文件 == 0 kb,则不执行任何操作

c# - 自定义 Visual Studio

c# - Windows Workflow Foundation - 获取已执行事件的列表?

javascript - 从js代码赋值给mvc razor的隐藏字段

c# - 接口(interface)、泛型和隐式操作

c# - 显示无/删除样式背后的asp.net 代码不起作用

c# - 在使用 Mono 的 Ubuntu 中运行基于 DevExpress 的 ASP.NET Web 应用程序时出错

C# .NET 套接字连接问题 - 通常只允许每个套接字地址使用一次

C# 无法将类型 'T' 隐式转换为 'object[*,*]'

c# - 返回 HelpText 类而不是字符串会意外地起作用