c# - Interface reference -


I doubt, I have searched a lot about it and I have not found anything that can explain.

I can have the property referenced to an interface inside my class, and could use DI to fill these properties. For example:

  public interface iTest {void DoSomething (); } Public Sector Exam {ITest _test; Public Test (Itte_ Test) {this._test = test; }}   

The problem is, if I have a generic interface, and my class does not use generic, when I make this property a compound error has increased < Former> Public Interface ITest & lt; T & gt; {Zero doSomething (T parameter); } Public Sector Exam {ITest & lt; T & gt; _testing; // Error (type can not be found) Public Test (Est T & gt; _test) {this._test = test; }}

Is this possible?

Your test class also needs to be generic - otherwise this thing There is no way to know what type of ITest & lt; T & gt; _test How do you know how to call _test.DoSomething () ? The type parameter of Test does not have to be T , of course:

  test of public class & lt; TFoo & gt; {ITest & lt; TFoo & gt; _testing; Public examination (ITate & Lt; TFU & gt; _test) {this._test = test; }}   

Then you can create it as:

  ITest & lt; String & gt; X = ...; The & lt; String & gt; Test = new test & lt; String & gt; (X);   

Type Pause by typing:

  test & lt; Int & gt; Test = new test & lt; Int & gt; (X);   

Because you have test & lt; Int & gt; a ITest & lt; String & gt; .

Alternatively, your test class may only need to take a specific type of ITest , and therefore can not be general:

  public class test {ITest & lt; Guid & gt; _testing; Public exam (ITeSte & Lt; Guid & gt; _test) {this._test = test; }}   

It all depends on what you are trying to achieve.

EDIT: As mentioned in the comments, if your test class ITest & lt; T & gt; Does not use any aspect of , which you trust on T , you want to create a non-normal base interface:

  Public Interaction Item {void DoSomethingBland (); } Public Interaction Eyetest & lt; T & gt; : ITest {Zero dozoomings specific (tfu); }   

Then you can only assign your class to ITest & lt; T & gt; instead of non-normal ITest can depend on the interface.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -