c# - Fulfilling a new() constraint on a generic method when using System.Object -
I am working with this third party libraries, and by tricking my mind to solve this particular issue I am here. I am implementing an interface that I need to pass the objects type object
, and to make a good number of calls, I need to pass them in a normal way, which is the class and Expect a type with New ()
Definitions are defined.
I know that through any object, I will fulfill these obstacles, but as far as I can see, there is no normal way that I can specify in a normal way. Satisfy the criteria One interface can not specify the constraints of the constraints, and an abstract class is not allowed in the form of a type argument for the general method.
The objects passed in this case are known and controlled by me, but the signature of both the removal methods can not be modified. Ideally I can implement the interface that guarantees the parameter-specific manufacturer criteria, but this does not seem to be an option.
Here is an example of what I'm talking about:
Public Zero Deletion (Object to be removed) {_repo.Delete (toDelete) // Sign here _repo.Delete & lt; T & gt; (T obj) where T: class, new ()}
To give some background and hopefully things will be explained - "delete" call ADO. Net data services have the implementation of IUpdatable
, while _repo.Delete & lt; T & gt; The
call is from subsonic
. I have a DataContextProvider
class that will handle these requests (and other similar types) for each class through my model, so it is not possible for a specific class direct artist I guarantee Classes are always classes and there is a parametric constructor, but I can not say to DataContext
that there is only a set set in the class which can be down - ideal I wanted as I DataContext
to work with the new classes without modifications.
You need to use reflection, you get the object to call on your repository Need to do it, and call it dynamically.
Note that this can be a very easy way to do this, but it seems to me that you have tried it, otherwise it would be a clear solution.
Since your "_promo" variable should be of a specific type, such as repository & lt; Employee & gt;
, you'll probably put it in this way?
_repo.Delete (Delilate as an employee); // or _repo.Delete ((employee) toDelete);
Or, if this is normal:
_repo.Delete (toDelete as T);
If that is not the option, then because of the code you have not shown, you need to resort to reflection.
The example is here:
using the system; Using the system. Namespace Console Application 14 {Public Class Program {Fixed Zero Main (String [] ARG) {Dummy D = New Dummy (); EntityType e = New EntityType (); D.Delete (E); Console.In.ReadLine (); }} Public Class EntityType {Public EntityType () {}} Public Class Dummy {Private Repository & lt; EntityType & gt; _repo = New repository & lt; EntityType & gt; (); Public Zero removal (object to delete) {type t = _repo.GetType (); Type generic type = t. Gategeneric aggregations () [0]; MethodInfo mi = t.GetMethod ("Delete", BindingFlag.Public | BindingFlag.Instance, Blank, New Type [] {Generic Type}, New Parameter Modifier [0]); // _repo.Delete (toDelete); Mi.Invoke (_repo, new object [] {toDelete}); }} Public Class Repository & lt; T & gt; Where T: square, new () {public zero removal (T-value) {Console.Out.WriteLine ("deleted:" + value); }}}
Comments
Post a Comment