Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# 约束与隐式算子_C#_Operators_Constraints - Fatal编程技术网

C# 约束与隐式算子

C# 约束与隐式算子,c#,operators,constraints,C#,Operators,Constraints,我有三个班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 i

我有三个班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);
    }
}
错在哪里

提前谢谢

错误:

  • 什么是CloneMemberwise()
  • 如果您的意思是
    MemberwiseClone()
    ,您不能在那里调用它。它是受保护的成员
  • 不能对类型调用属性。这里:
    T.Firstname
    T.Lastname
    ,等等
  • 它是
    Debug
    ,而不是
    Degub
  • 您不会将值/引用传入
    Execute
    方法
  • A、B或C都不是从
    Generic\u T
    派生的,因此对Execute方法的约束将失败
  • 类名不以
    ()
    结尾。你有
    class A()
    ,我已经重新编辑过了
  • A
    或任何类不能自动转换为
    Generic\T
  • 您应该使用继承,我怀疑
    Generic\T
    应该是基类
  • 你没有具体的问题
  • 可能的建议:

    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的大多数介绍性文本中解释。不理解它们,会让你的生活很痛苦。

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

    interface IFoo 
    {
       int ID {get; set;}
       string LastName {get; set;}
       string FirstName {get; set;}
    }
    
    然后,您的execute方法将如下所示:

    public override Execute<T>(T obj) where T: new(), IFoo
    { 
        Debug.WriteLine(obj.Firstname + ”, “ + obj.Lastname + “, “ + obj.ID);
    }
    
    public override Execute(T obj),其中T:new(),IFoo
    { 
    Debug.WriteLine(obj.Firstname+“,“+obj.Lastname+”,“+obj.ID”);
    }
    
    我对您的代码做了一些修改并重新格式化,但代码根本无效,无法编译。有几个错误,所以我可以回答您的问题。请提供一个真实的示例。您显然是在这里键入的。复制/粘贴实际代码
    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);
    }