CS0553-为什么? [英] CS0553 - Why?

查看:56
本文介绍了CS0553-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不允许用户定义的向基类或从基类的转换.

User defined conversions to or from a base class are not allowed.

好吧

最重要的是,运算符只是使用该语言的另一种方式.我有运算符,可以在各处转换类型,因此我可以执行++或* =或>>>.在矩形上,让它处理我想要的东西.这 突然之间,C#会告诉我我对操作员不能做什么,事实就是bull @#$%.

first and foremost and operator is just another way of using the language.  I have operators that transform types all over the place, so i can do a ++ or *= or a >>> on a rectangle and have it process what i want it to do.  The fact that suddenly C# is going to tell me what I can or can't do with an operator is bull@#$%.

但是话虽这么说,为什么我不能再从基类转换了?我可以使用VS 2013,但在2015年出现此BS错误.我的意思是,我可以理解这是警告还是某些代码分析最佳做法"?问题.但是要完全 拒绝我使用该功能? WTF?

But that being said, why can't I convert from a base class anymore?  Is VS 2013 I could, but in 2015 I get this BS error.  I mean, I can understand if it was a Warning, or some Code Analysis "best practice" issue.  But to completely deny me the functionality?  WTF?

public class Parent
{
  public Parent(string name)
  {
    this.Name = name;
  }

  public virtual string Name { get; set; }
}

public class Child : Parent
{
  public Child(string name) : base(name) {}

  public string FirstName { get; set; }
  public string LastName { get; set; }

  public override string Name
  {
    get { return String.Format("{0}, {1}", this.LastName, this.FirstName); }
    set 
    {
      if (String.IsNullOrEmpty(value))
      {
        this.FirstName = this.LastName = String.Empty;
      }
      else 
      {
        string[] parts = value.Split(',');
        if (parts.Length == 2) 
        {
          this.FirstName = parts[1].Trim();
          this.LastName = parts[0].Trim();
        }
        else if (parts.Length == 1)
          this.FirstName = value;
      }
    }
  }
}

void Main()
{
  Parent p = new Parent() { Name = "Smith, Gordon" };
  Child c = new Child() { Name = "Doe, John" };

  List<Parent> parents = new List<Parent>();
  parents.Add(p);

  List<Child> children = new List<Child>();
  children.Add(c);

  parents.Add(c); // Works.  Not a problem
  children.Add(p); // This fails.  so a completely valid case for a conversion.
  //After adding the conversion
  children.Add((Child)p); //this is now valid
}

//so I would always in the past add this to Child:
public static explicit operator Child(Parent p) 
{ 
  return p != null ? new Child(p.Name) : null;
}

这在2013年及以前是100%有效的,为什么现在突然无效了.这显然是需要将Parent转换为CHild的需要,但是现在C#代替了Operator,迫使我使用其他方法(无论是用作构造函数还是重新创建) 我直接代码中的对象.用户定义的显式运算符的目的是简化转换转换.

This was 100% valid in 2013 and before, why is it suddenly NOT valid now.  THis is a demonstrable need to convert Parent into CHild, but now instead of an Operator, C# is FORCING me to use some other method, whether as a constructor, or to recreate the objects in my direct code.  The purpose of the user-defined explicit operator is to simplify the transform conversion.

为什么C#不断对REMOVE功能进行重大更改.一定要添加新的做事方式,但要避免破坏行之有效且被广泛使用的事物.

Why is C# continually making breaking changes the REMOVE functionality.  By all means add new ways of doing things but quit breaking things that work and are widely used.

Jaeden"Sifo Dyas" al'Raec Ruiner

Jaeden "Sifo Dyas" al'Raec Ruiner

永远不要信任计算机.您的大脑比任何微芯片都要聪明."
PS-不要在其他人的问题上标记答案.诸如假期和假期之类的事情可能会减少及时的活动,并且直到提出问题的人能够测试您的答案之前,仅因为您认为正确,这是不正确的.标记它 为他们正确设置通常会阻止其他人阅读问题,甚至可能无法提供真正的正确"配置.

"Never Trust a computer. Your brain is smarter than any micro-chip."
PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.

推荐答案

现在我只是怀疑是否可以在Visual Studio 2013中实现,但我没有对其进行测试.

right now I simply doubt that this was possible in Visual Studio 2013 but I didn't test it.

为什么不可能?让我们创建一个示例:

Why is it not possible? Let us just create an example:

让我们以您的父/子示例为例,让我们想象一下,将允许使用这样的显式强制转换运算符.

Lets take your Parent / Child example and let us imagine, that such an explicit cast operator would be allowed.

让我们采用以下代码:

父母p = new Child(); //可以,子级是从父级派生的,所以每个子级也是父级.
子c =(子)p; //那么这里应该发生什么?

Parent p = new Child(); // That is ok, Child derived from Parent so each Child is also a Parent.
Child c = (Child) p; // So what should happen here?

您现在创建的核心问题是,您有2种可能的操作可以在此处触发.引用可以简单地投给一个孩子.那会很好的.或者您可以使用显式运算符来创建新实例.

The core problem that you created now is, that you have 2 possible actions that could be triggered here. The reference could simply be cast to a child. That will work fine. Or your explicit operator could be used which would create new instance. 

使用这种显式运算符进行设计是我根本不喜欢的.过去,我看到了用于获取包装了句柄的类的实例的代码.所以有类似的东西

And the design with this explicit operators are something that I don't like at all. I saw code in the past that used it to get instances of classes that wrapped a handle. So there was stuff like

Window window = someHandle; //无需强制转换(Window).

Window window = someHandle; // No need to have a (Window) cast.

该句柄可能来自窗口,但是在查看某些代码时,这样的分配"看起来根本不正确.

The handle might be from a window but such an Assignment simply does not look correct when looking at some code.

Window window = new Window(someHandle);

Window window = new Window(someHandle);

更好,并且不会隐藏重要的实现细节.

is much better and does not hide important implementation details.

但是我同意-这是开发人员的观点,语言不应该限制开发人员.但我希望技术原因可以解释核心问题.

But I agree - that is the opinion of a developer and a language should not limit a developer. But I hope the technical reason explains the core problem.

以诚挚的问候,

Konrad


这篇关于CS0553-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆