【问题标题】:Constraints and implicite operators约束和隐式运算符
【发布时间】:2011-02-22 05:58:48
【问题描述】:

我有 A、B 和 C 三个班级, 有些属性在所有三个类中,有些则不在

public class A
{ 
    public string Firstname {get; set;}
    public string  Lastname {get; set;}
    public int ID {get; set;}
    public int xxx {get; set}   // only in class A
    public int yyy{get; set}    // only in class A
    ... 
}

public class B
{ 
    public string Firstname {get; set;}
    public string  Lastname {get; set;}
    public int ID {get; set;}
    public int aaa {get; set} // only in class B
    public int bbb {get; set} // only in class B
    ... 
}

public class C
{ 
    public string Firstname {get; set;}
    public string  Lastname {get; set;}
    public int ID {get; set;}
    public int kkk {get; set} // only in class C
    public int ppp {get; set} // only in class C
    ...
}

我想调用 XYZ 类的 Execute 方法...

public class XYZ
{
    public override Execute<T>() where T: Generic_T, new()
    { 
      T abc = new T();
      ...
      Debug.WriteLine(abc.Firstname + ”, “ + abc.Lastname + “, “ + abc.ID);
    }
}

...包含所有三个类,例如:

XYZ x1 = new XYZ();
XYZ.Execute<A>();

XYZ x2 = new XYZ();
XYZ.Execute<B>();

XYZ x3 = new XYZ();
XYZ.Execute<C>();

我的想法行不通:

public class Generic_T
{
    public static implicit operator A(Generic_T x)
    { 
        return (A)x.MemberwiseClone();
    }
}

错在哪里?

提前致谢!

【问题讨论】:

  • 我修正了你的代码并重新格式化,但代码根本无效,它不会编译。有几个错误,所以我可以说出你想要回答的内容。
  • 请提供一个真实的例子。你显然是在这里输入的。复制/粘贴实际代码

标签: c# operators constraints


【解决方案1】:

错误:

  1. CloneMemberwise() 是什么?
  2. 如果你的意思是MemberwiseClone(),你不能在那里打电话。它是protected 成员。
  3. 您不能调用类型的属性。此处:T.Firstname、T.Lastname 等。
  4. 是Debug,不是Degub。
  5. 您没有将值/引用传递给Execute 方法。
  6. A、B 或 C 都不是从 Generic_T 派生的,因此对 Execute 方法的约束将失败。
  7. 类名不以() 结尾。你有 class A() 我已经重新编辑过。
  8. A 或任何类都不能自动转换为Generic_T。
  9. 你应该使用继承,我怀疑Generic_T 应该是一个基类。
  10. 您没有具体问题

可能的建议:

public abstract class Generic_T
{
    public string Firstname {get; set;}
    public string  Lastname {get; set;}
    public int ID {get; set;}
}

public class A : Generic_T
{ 
    public int xxx {get; set}   // only in class A
    public int yyy{get; set}    // only in class A
    ... 
}

public class B : Generic_T
{ 
    public int aaa {get; set}   // only in class B
    public int bbb {get; set} // only in class B
    ... 
}

public class C : Generic_T
{ 
    public int kkk {get; set}   // only in class C
    public int ppp {get; set} // only in class C
    ...
}

也没有理由将任何派生类转换为Generic_T,因为它们已经是Generic_T 的实例。

所有这些信息通常在 C#/.NET 的大多数介绍性文本中都有解释。不理解它们,会让你的生活变得悲惨。

【讨论】:

    【解决方案2】:

    不知道为什么在代码中需要 Generic_T。 要强制执行工作,您需要实现以下接口

    interface IFoo 
    {
       int ID {get; set;}
       string LastName {get; set;}
       string FirstName {get; set;}
    }
    

    那么你的执行方法将如下所示:

    public override Execute<T>(T obj) where T: new(), IFoo
    { 
        Debug.WriteLine(obj.Firstname + ”, “ + obj.Lastname + “, “ + obj.ID);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      相关资源
      最近更新 更多