开发者

Constraints and implicite operators

I have three classes A,B and C, some properties are in all three classes, some not

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
    ...
}

I want to call the Execute method of class XYZ ...

public class XYZ
{
    public override Execute<T>() where T: Generic_T, new()
    { 
      T abc = new T();
      ...
      Debug.WriteLine(abc.Firstname + ”, “ + abc.开发者_运维技巧Lastname + “, “ + abc.ID);
    }
}

... with all three classes,like:

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

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

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

My idea doesn’t work:

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

Where is the mistake?

Thanks in advance!


Mistakes:

  1. What is CloneMemberwise() ?
  2. If you meant MemberwiseClone(), you can't call it there. It is a protected member.
  3. You cannot call properties on a type. Here: T.Firstname, T.Lastname, etc.
  4. It is Debug, not Degub.
  5. You do not pass a value/reference in to the Execute method.
  6. Neither A, B or C derives from Generic_T, so the constraint on the Execute method will fail.
  7. Class names do not end with (). You had class A() which I re-edited already.
  8. A or any class cannot be automagically converted to Generic_T.
  9. You should be using inheritance, I suspect Generic_T should be a base class.
  10. You do not have a specific question

Possible suggestion:

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
    ...
}

Also there is not reason to convert any of the derived classes to Generic_T as they as an instance of Generic_T already.

All of this information is normally explained in most introductory texts for C#/.NET. Failure to understand them, will make your life miserable.


Don't know why do you need Generic_T in your code. To force Execute work you need to implement following interface

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

Then your execute method will look like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜