Advertisement
progerz

Untitled

Nov 23rd, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Reflection;
  7.  
  8. namespace ClassCollection
  9. {
  10.  
  11.     public interface IPlugin<T>
  12.     {
  13.         T Converting(T input);
  14.     }
  15.  
  16.     class StringUpper : IPlugin<string>
  17.     {
  18.         public string Converting(string input)
  19.         {
  20.             return input.ToUpper();
  21.         }
  22.     }
  23.  
  24.     class StringLower : IPlugin<string>
  25.     {
  26.         public string Converting(string input)
  27.         {
  28.             return input.ToLower();
  29.         }
  30.     }
  31.  
  32.     class StringTransform : IPlugin<string>
  33.     {
  34.         public string Converting(string input)
  35.         {
  36.             return input += "\nYou shall not pass!";
  37.         }
  38.     }
  39.  
  40.     class Program
  41.     {
  42.         static void Main(string[] args)
  43.         {
  44.             Console.WriteLine("Class in the action");
  45.             string @namespace = "ClassCollection";
  46.             var ruleType = typeof(IPlugin<string>);
  47.             AppDomain.CurrentDomain.GetAssemblies()
  48.                        .SelectMany(t => t.GetTypes())
  49.                        .Where(t => t.IsClass && t.Namespace == @namespace);
  50.             var q = from t in Assembly.GetExecutingAssembly().GetTypes()
  51.                     where t.IsClass && t.Namespace == @namespace && ruleType.IsAssignableFrom(t) && t.Name != "StringCollection"
  52.                     select t;
  53.             var a = q.ToList();
  54.            
  55.             q.ToList().ForEach(t =>
  56.             {
  57.                 /* try
  58.                 {
  59.                     var myObj = Activator.CreateInstance("ClassCollection", t.Name);
  60.                 }
  61.                 catch (Exception e)
  62.                 {
  63.                     Console.WriteLine("Some Error {0}", e);
  64.                    
  65.                 }*/
  66.  
  67.                 Type magicType = Type.GetType(t.Name);
  68.                 if (magicType != null)
  69.                 {
  70.                     MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
  71.                     object classInstance = Activator.CreateInstance(magicType, null);
  72.  
  73.                     Console.WriteLine("Transformed : '{0}'", magicMethod.Invoke(classInstance, new object[] { "blabla" }));
  74.                 }
  75.                 else { Console.WriteLine("Errooooooooooooor"); }
  76.             });
  77.  
  78.  
  79.             Console.WriteLine("Press any key to exit.");
  80.             Console.ReadKey();
  81.  
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement