Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try using a factory pattern.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace SO { class Program { static void Main(string[] args) { MessageFactory factory = new MessageFactory(); IMessage msg = factory.CreateObject(1); IMessage msg2 = factory.CreateObject(2); } } public interface IMessage { short Message_ID { get; } } public class Generic_Message1 : IMessage { public short Message_ID { get { return ID; } } internal const short ID = 1; } public class center_message1 : IMessage { public short Message_ID { get { return ID; } } internal const short ID = 2; } public class MessageFactory { private Dictionary&lt;short, Type&gt; messageMap = new Dictionary&lt;short, Type&gt;(); public MessageFactory() { Type[] messageTypes = Assembly.GetAssembly(typeof(IMessage)).GetTypes(); foreach (Type messageType in messageTypes) { if (!typeof(IMessage).IsAssignableFrom(messageType) || messageType == typeof(IMessage)) { // messageType is not derived from IMessage continue; } IMessage message = (IMessage)Activator.CreateInstance(messageType); messageMap.Add(message.Message_ID, messageType); } } public IMessage CreateObject(short Message_ID, params object[] args) { return (IMessage)Activator.CreateInstance(messageMap[Message_ID], args); } } } </code></pre> <p><strong>EDIT to answer comment:</strong></p> <p>If the "generic" library is the one processing the messages, and it has no knowledge of the types of message is is processing, you obviously have to change that.</p> <p>Either move to a "plug-in" model where your custom message dlls will be loaded from a specific directory on startup of the generic library, or read the custom message dlls from a config file at startup for the generic library.</p> <pre><code>// Read customMessageDllName and customMessageClassName from your config file Assembly assembly = Assembly.Load(customMessageDllName); IMessage customMessage = (IMessage)assembly.CreateInstance(customMessageClassName); </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