c# - Generic contraints -
i've got class want enforce generic constraints on when created
myobject.cs
public class myobject<t> t : objectbase, new() { public myobject() { } public bool validate(out serviceerror? message) { // validation } }
objectbase.cs
public abstract class objectbase { public abstract bool validate(out serviceerror? message); }
i'm wondering if there way avoid having place generic constraints t every time have function such this:
objectrepo.cs
public void put<t>(myobject<t> obj) t : objectbase, new() { // code }
if constraints t specified in myobject class, there reason have re-specify every single time use myobject parameter?
don't redefine t on method if have on class
public class myobject<t> t : objectbase, new() { //public void put<t>(myobject<t> obj)//doesn't work public void put(myobject<t> obj)//works { // code } }
update
if put in class best solution refactor code , move t related method in same class (and add constrain on class)
update 2
if update 1 not possible ,it might worth checking if can replace t objectbase or base class
Comments
Post a Comment