.net - Generic type conversion -


i'm having serious design problems due generics issues. perhaps has suggestions.

edit: so, know not done, i've changed example code, because i've realized original pseudo-code did not explain problem. following code more closely resembles real example dealing with. hope problem clearer it. apologize ahead bit lengthy, experience, problems generics show when try build more complex structure. so:

class program     {         static void main(string[] args)         {             iconnector<iservice> connector = connectorbuilderfactory.newbuilder<iservice>("someendpoint").makereliable().getconnector();             connector.connect();         }     }      public interface iservice : iconnectionmaintainable     {         void dosomething();     }      public interface iconnectionmaintainable     {         datetime getservertime();     }      public interface iconnector<t>     {         t channel { get; }         void connect();         void disconnect();     }      public interface iconnectorbuilder<t>     {         iconnector<t> getconnector();         iconnectorbuilder<t> makereliable();         // ...more connector-configuration methods     }      public class channelwatchdog<t> t : iconnectionmaintainable     {         private iconnector<t> connector;          public channelwatchdog(iconnector<t> connector /*various other parameters*/)         {             this.connector = connector;         }          // ...methods use connector's connect, disconnect, , getservertime methods     }      public class connector<t> : iconnector<t>     {         private t channel;          public connector(string endpoint)         {             // ...build channel         }          public t channel         {             { return channel; }         }          public void connect()         {             // ...connect server         }          public void disconnect()         {             // ...disconnect server         }     }      public class connectorbuilder<t> : iconnectorbuilder<t>     {         private string endpoint;          public connectorbuilder(string endpoint)         {             this.endpoint = endpoint;         }          public iconnector<t> getconnector()         {             connector<t> connector = new connector<t>(endpoint);              // if reliability requested, build channelwatchdog: following line not compile:             // channelwatchdog<t> watchdog = new channelwatchdog<t>(connector);              return connector;         }          public iconnectorbuilder<t> makereliable()         {             // save various parameters required build channelwatchdog             return this;         }     }      public static class connectorbuilderfactory     {         public static iconnectorbuilder<t> newbuilder<t>(string endpoint)         {             return new connectorbuilder<t>(endpoint);         }     } 

so, firstly, if find getconnector method in connectorbuilder class, see commented line of code, not compile if uncommented. line essence of problem. problem might obvious code, try explain anyway in case not:

  1. i have internal class (channelwatchdog) needs iconnector. not iconnector, iconnector, because other non-generic iconnector methods, needs getservertime method iconnectionmaintainable interface.

  2. to simplify construction of connectors, hoped implement builder using expression builder pattern (the iconnectionbuilder interface). however, want able construct iconnector, not iconnector<iconnectionmaintainable>. therefore, cannot constrain t in iconnectorbuilder in same way constrain channelwatchdog. lacking constraint, have no way build when getconnector called. adding constraint makereliable method not help.

so, reason posted question wanted apparently impossible. wanted channelwatchdog , connectorbuilder class this:

public class channelwatchdog     {         private iconnector<iconnectionmaintainable> connector;          public channelwatchdog(iconnector<iconnectionmaintainable> connector /*various other parameters*/)         {             this.connector = connector;         }          // ...methods use connector's connect, disconnect, , getservertime methods     }      public class connectorbuilder<t> : iconnectorbuilder<t>     {         private string endpoint;          public connectorbuilder(string endpoint)         {             this.endpoint = endpoint;         }          public iconnector<t> getconnector()         {             connector<t> connector = new connector<t>(endpoint);              // if reliability requested, build channelwatchdog: following line not compile:             channelwatchdog watchdog = new channelwatchdog((iconnector<iconnectionmaintainable>)connector);              return connector;         }          public iconnectorbuilder<treliable> makereliable<treliable>() treliable : t, iconnectionmaintainable         {             // save various parameters required build channelwatchdog             return (iconnectorbuilder<treliable>)this;         }     } 

but cast iconnector fails @ runtime.

so more lengthy had intended. if you've gotten far in reading have :) ideas welcome, including restructuring code.

btw, having not found solution myself, created different connectorbuilders (in case, reliableconnectorbuilder) , different factory methods in factory. don't solution much.

edit: clarify , reiterate: cannot constrain iconnector nor connectionbuilder because these need support cases in iconnectionmaintainable interface not implemented.

code interface?

genericclass<ifoo> wrapper = new genericclass<ifoo>(new fooimplementor()); acceptor acceptor = new acceptor(wrapper); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -