Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using your own question, couldn't you just use an interface?</p> <pre><code>public interface IMyControl&lt;T&gt; where T:class { T Field { get; set;} event EventHandler&lt;MyEventArgs&lt;T&gt;&gt; MyEvent; } public class MyControl&lt;T&gt; : UserControl, IMyControl&lt;T&gt; where T:class { public T Field { get; set;} public event EventHandler&lt;MyEventArgs&lt;T&gt;&gt; MyEvent; } </code></pre> <p>And then to use :</p> <pre><code>// (which equates to IMyControl control = ...) var control = condition ? new MyControl&lt;ClassA&gt;() : new MyControl&lt;ClassB&gt;(); control.Field = null; control.Enabled = false; </code></pre> <p>This saves you having multiple classes for each Type parameter you want to use and therefore encourages code reuse and follows polymorphism (OOP).</p> <p>Hope that is helpful!</p> <p><strong>EDIT</strong></p> <p>OK - so Alex was right and I shouldn't have fired from the hip on that one! After a bit of investigation this seems to be a lot harder than I thought! So I began to wonder if what you're asking is the right way to go. Now, whether it is the sort of answer you want or not, I don't know, but here it is for you to ponder.</p> <p>Basically, donn't use generics. It makes life really really difficult in this scenario! Instead, hide behind interfaces. The code below achieves the same as you're trying to above, but with the exception that you now have field tied to a 'concrete' interface. But realistically, you want this - you should have known behaviours and patterns, otherwise, to my mind, you don't know what your system should be doing and therefore there's something wrong with your requirements. Anyway, surmon over, here's some code!</p> <pre><code>public interface IMyClass { int I { get; set; } string S { get; set; } } public interface IMyControl { IMyClass Field { get; set; } event EventHandler&lt;EventArgs&gt; MyEvent; } public class MyControl : UserControl, IMyControl { public MyControl(IMyClass field) { this.Field = field; } public IMyClass Field { get; set; } public event EventHandler&lt;EventArgs&gt; MyEvent; } public class MyClass1 : IMyClass { public int I { get; set; } public string S { get; set; } public override string ToString() { return "I am a MyClass1!!!"; } } public class MyClass2 : IMyClass { public int I { get; set; } public string S { get; set; } public override string ToString() { return "I am a MyClass2!!!"; } } var control = someCondition ? new MyControl(new MyClass1()) : new MyControl(new MyClass2()); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload