Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- namespace ClassCollection
- {
- public interface IPlugin<T>
- {
- T Converting(T input);
- }
- class StringUpper : IPlugin<string>
- {
- public string Converting(string input)
- {
- return input.ToUpper();
- }
- }
- class StringLower : IPlugin<string>
- {
- public string Converting(string input)
- {
- return input.ToLower();
- }
- }
- class StringTransform : IPlugin<string>
- {
- public string Converting(string input)
- {
- return input += "\nYou shall not pass!";
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Class in the action");
- string @namespace = "ClassCollection";
- var ruleType = typeof(IPlugin<string>);
- AppDomain.CurrentDomain.GetAssemblies()
- .SelectMany(t => t.GetTypes())
- .Where(t => t.IsClass && t.Namespace == @namespace);
- var q = from t in Assembly.GetExecutingAssembly().GetTypes()
- where t.IsClass && t.Namespace == @namespace && ruleType.IsAssignableFrom(t) && t.Name != "StringCollection"
- select t;
- var a = q.ToList();
- q.ToList().ForEach(t =>
- {
- /* try
- {
- var myObj = Activator.CreateInstance("ClassCollection", t.Name);
- }
- catch (Exception e)
- {
- Console.WriteLine("Some Error {0}", e);
- }*/
- Type magicType = Type.GetType(t.Name);
- if (magicType != null)
- {
- MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
- object classInstance = Activator.CreateInstance(magicType, null);
- Console.WriteLine("Transformed : '{0}'", magicMethod.Invoke(classInstance, new object[] { "blabla" }));
- }
- else { Console.WriteLine("Errooooooooooooor"); }
- });
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement