Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I hope I understand you correctly.</p> <p>The class you create in the Type project represent some objects that have different behaviors but the same data members and you would like to be able to use these easily within your projects without the hassle of having to explicitly list these objects.</p> <p>I would create some base interface that all my objects in the Types project would implement. I would then use a Factory Class that would use reflection to collect all objects that implement said interface.</p> <pre><code>public interface iFoo { string FoundItem { get; set; } string Expression { get; } string Value { get; set; } void sharedFunctionName(); } public static class FooFactory { public static List&lt;iFoo&gt; GetTypeList() { List&lt;iFoo&gt; types = new List&lt;iFoo&gt;(); types.AddRange(from assembly in AppDomain.CurrentDomain.GetAssemblies() from t in assembly.GetTypes() where t.IsClass &amp;&amp; t.GetInterfaces().Contains(typeof(iFoo)) select Activator.CreateInstance(t) as iFoo); return types; } } </code></pre> <p>Then your Reader would receive all necessary information for the supported types without you having to manually dictate it anymore.</p> <p>Since I guess the value type would be different at some point, you could use a Generic Interface like this :</p> <pre><code>public interface iFoo { string FoundItem { get; set; } string Expression { get; } void sharedFunctionName(); } public interface iFoo&lt;T&gt; : iFoo { T Value { get; set; } } public static class FooFactory { public static List&lt;iFoo&gt; GetTypeList() { List&lt;iFoo&gt; types = new List&lt;iFoo&gt;(); types.AddRange(from assembly in AppDomain.CurrentDomain.GetAssemblies() from t in assembly.GetTypes() where t.IsClass &amp;&amp; t.GetInterfaces().Contains(typeof(iFoo)) select Activator.CreateInstance(t) as iFoo); return types; } } public class FooBar : iFoo&lt;int&gt; { } </code></pre> <p>In this example the base interface iFoo is kept to ease the discovery process. Using generic interface would allow to kept your code Type safe (as opposed to using a Value of type object) but you will have to add some logic when recovering your objects to be able to access your Value properly.</p> <p>Plus, if you ever need to create functions that would require to be shared within all your objects you would be able to add extension methods within the Factory Class and Voilà.</p> <p>EDIT:</p> <p>Based on the new information:</p> <p>Your Types correspond to a type of data that you will find in a file based on some regular expression. There might be different type of transformation based on the user selection and the Type.</p> <p>We know that the user will have to pick a mode from a list and this will affect the transformation to apply on the Types.</p> <p>So this is what I would do : I would move the transformation logic right into the Type class, polymophism will take care of exactly which transformation will be called.</p> <p>I would put the RegularExpression to use to detect the Type into the Type itself, this will allow you to use reflection and the Factory class discuss earlier more easily.</p> <p>This way, everything is standard. Your reader is aware of any new type you create in the type project without manual intervention and once detected the right transformation could be applied and the original string is always accessible.</p> <pre><code>public enum UserMode {Mode1, Mode2}; public interface iType { string Expression {get;} string OriginalString {get; set;} string Transform(UserMode Mode); iType getNewInstance(string OriginalString); } public class Type1 : iType { public string Expression {get { return "RegularExpression"; }} public string OriginalString {get; set;} //Add any other private members you need to accomplish your work here. public string Transform(UserMode Mode) { switch(Mode) { case UserMode.Mode1: //write the transformation code for this scenario return ResultString; break; } } public iType getNewInstance(string Original) { return (iType)(new Type1(){ OriginalString = Original }); } } public static class TypeFactory { public static List&lt;iType&gt; GetTypeList() { List&lt;iType&gt; types = new List&lt;iType&gt;(); types.AddRange(from assembly in AppDomain.CurrentDomain.GetAssemblies() from t in assembly.GetTypes() where t.IsClass &amp;&amp; t.GetInterfaces().Contains(typeof(iType)) select Activator.CreateInstance(t) as iType); return types; } } </code></pre> <p>Now, all you will have to do if match the expression from the iTypes in the list. When you have a match you do :</p> <pre><code>var TransformationReady = from t in TypeFactory.GetTypeList() where Regex.IsMatch(YourFileLine, t.Expression) select t.getNewInstance(Regex.Match(YourFileLine, t.Expression)); </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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