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:
- What is
CloneMemberwise()
? - If you meant
MemberwiseClone()
, you can't call it there. It is aprotected
member. - You cannot call properties on a type. Here:
T.Firstname
,T.Lastname
, etc. - It is
Debug
, notDegub
. - You do not pass a value/reference in to the
Execute
method. - Neither A, B or C derives from
Generic_T
, so the constraint on the Execute method will fail. - Class names do not end with
()
. You hadclass A()
which I re-edited already. A
or any class cannot be automagically converted toGeneric_T
.- You should be using inheritance, I suspect
Generic_T
should be a base class. - 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);
}
精彩评论