Exemplo n.º 1
0
        // NOTE Before making this call make sure that MyInterface assembly is signed with mykey.snk file. See Signing tab in MyInterface project properties editor.
        public IDoSomething Load <T>(string assemblyString)
        {
            // TODO Load an assembly using assemblyString and a static method of Assembly class.
            Assembly assembly = null;

            // TODO Get all types that are loaded in the assembly.
            Type[] types = null;

            // TODO Get a type instance of the IDoSomething interface.
            Type interfaceType = null;

            // TODO Get a type instance of the DoSomethingAttribute class.
            Type attributeType = null;

            // Find first type that has DoSomething attribute (use GetCustom... method) and implements IDoSomething (use IsAssignable method).
            Func <Type, bool> implementsInterfaceAndHasAttribute = (Type t) =>
            {
                return
                    // TODO Uncomment the next line and put correct method name there. Remove false.
                    // interfaceType.MethodName(t)
                    (false
                     &&
                     // TODO Uncomment the next line and put correct method name there. Remove false.
                     // t.MethodName().Any(t2 => t2.GetType() == attributeType);
                     false);
            };

            // TODO Use appropriate LINQ method on types array with implementsInterfaceAndHasAttribute delegate to find the type that matches all criteria. Only one type is expected, exception should be thrown otherwise.
            Type doSomethingType = null;

            // TODO Create an instance of doSomethingType using static method of Activator class.
            IDoSomething doSomethingService = (IDoSomething)null;

            return(doSomethingService);
        }
Exemplo n.º 2
0
 public HomeController(
     IDoSomething doSomething,
     IRepository<Person> personRepository 
     )
 {
     _personRepository = personRepository;
     _doSomething = doSomething;
 }
    static void Main()
    {
        IDoSomething worker = InsultMe.Instance;

        worker.DoWhateverItIsThatIDo();

        worker = InspireMe.Instance;
        worker.DoWhateverItIsThatIDo();
    }
Exemplo n.º 4
0
 private static void Test(IDoSomething oDoesSomething)
 {
     IntVec oVec = new IntVec();
     for (oVec.x = 0; oVec.x < 2; oVec.x++)
     {
         for (oVec.y = 0; oVec.y < 2; oVec.y++)
         {
             oDoesSomething.Do(oVec);
         }
     }
 }
Exemplo n.º 5
0
        private static void Test(IDoSomething oDoesSomething)
        {
            IntVec oVec = new IntVec();

            for (oVec.x = 0; oVec.x < 2; oVec.x++)
            {
                for (oVec.y = 0; oVec.y < 2; oVec.y++)
                {
                    oDoesSomething.Do(oVec);
                }
            }
        }
Exemplo n.º 6
0
        // LoadFrom() goes through Fusion and can be redirected to another assembly at a different path
        // but with that same identity if one is already loaded in the LoadFrom context.
        // More details: http://stackoverflow.com/questions/1477843/difference-between-loadfile-and-loadfrom-with-net-assemblies
        public Result LoadFrom(string fileName, Input data)
        {
            var assembly = Assembly.LoadFrom(fileName);
            var type     = assembly.GetTypes();

            // TODO: Find first type that has DoSomething attribute and implements IDoSomething.
            // TODO: Create an instance of this type.

            IDoSomething doSomethingService = null; // TODO Save instance to variable.

            return(doSomethingService.DoSomething(data));
        }
Exemplo n.º 7
0
        // More details: http://stackoverflow.com/questions/1477843/difference-between-loadfile-and-loadfrom-with-net-assemblies
        public Result LoadFrom(string fileName, Input data)
        {
            var assembly = Assembly.LoadFrom(fileName);
            var types    = assembly.GetTypes();

            Type type = types.FirstOrDefault(t => t.GetCustomAttribute <DoSomethingAttribute>() != null && !typeof(IDoSomething).IsAssignableFrom(t));
            // TODO: Find first type that has DoSomething attribute and implements IDoSomething.
            // TODO: Create an instance of this type.

            IDoSomething doSomethingService = null; // TODO Save instance to variable.


            return(doSomethingService.DoSomething(data));
        }
Exemplo n.º 8
0
        // More details: http://stackoverflow.com/questions/1477843/difference-between-loadfile-and-loadfrom-with-net-assemblies
        public Result LoadFrom(string fileName, Input data)
        {
            var assembly = Assembly.LoadFrom(fileName);
            var types    = assembly.GetTypes();

            // TODO: Find first type that has DoSomething attribute and implements IDoSomething.
            var typeIDoSmth = types.Where(t => t.GetInterface("IDoSomething") != null && Attribute.IsDefined(t, typeof(DoSomethingAttribute))).FirstOrDefault();
            // TODO: Create an instance of this type.
            var instance = Activator.CreateInstance(typeIDoSmth);

            IDoSomething doSomethingService = instance as IDoSomething; // TODO Save instance to variable.

            return(doSomethingService.DoSomething(data));
        }
        // Before making this call make sure that MyInterface assembly is signed with mykey.snk file. See Signing tab in MyInterface project properties editor.
        // Usage:
        // result = loader.Load("MyLibrary, Version=1.2.3.4, Culture=neutral, PublicKeyToken=f46a87b3d9a80705", input)
        public Result Load(string assemblyString, Input data)
        {
            // LoadFile() doesn't bind through Fusion at all - the loader just goes ahead and loads exactly what the caller requested.
            // It doesn't use either the Load or the LoadFrom context.
            // LoadFile() has a catch. Since it doesn't use a binding context, its dependencies aren't automatically found in its directory.

            var assembly = Assembly.Load(assemblyString);
            var types    = assembly.GetTypes();

            // TODO: Find first type that has DoSomething attribute and implements IDoSomething.
            // TODO: Create an instance of this type.

            IDoSomething doSomethingService = null; // TODO Save instance to variable.

            return(doSomethingService.DoSomething(data));
        }
Exemplo n.º 10
0
        // Before making this call make sure that MyInterface assembly is signed with mykey.snk file. See Signing tab in MyInterface project properties editor.
        // Usage:
        // result = loader.Load("MyLibrary, Version=1.2.3.4, Culture=neutral, PublicKeyToken=f46a87b3d9a80705", input)
        public Result Load(string assemblyString, Input data)
        {
            // LoadFile() doesn't bind through Fusion at all - the loader just goes ahead and loads exactly what the caller requested.
            // It doesn't use either the Load or the LoadFrom context.
            // LoadFile() has a catch. Since it doesn't use a binding context, its dependencies aren't automatically found in its directory.

            var assembly = Assembly.Load(assemblyString);
            var types    = assembly.GetTypes();

            // TODO: Find first type that has DoSomething attribute and implements IDoSomething.
            var typeIDoSmth = types.Where(t => t.GetInterface("IDoSomething") != null && Attribute.IsDefined(t, typeof(DoSomethingAttribute))).FirstOrDefault();
            // TODO: Create an instance of this type.
            var instance = Activator.CreateInstance(typeIDoSmth);

            IDoSomething doSomethingService = instance as IDoSomething; // TODO Save instance to variable.

            return(doSomethingService.DoSomething(data));
        }
Exemplo n.º 11
0
        // Before making this call make sure that MyInterface assembly is signed with mykey.snk file. See Signing tab in MyInterface project properties editor.
        // Usage:
        // result = loader.Load("MyLibrary, Version=1.2.3.4, Culture=neutral, PublicKeyToken=f46a87b3d9a80705", input)
        public Result Load(string assemblyString, Input data)
        {
            // LoadFile() doesn't bind through Fusion at all - the loader just goes ahead and loads exactly what the caller requested.
            // It doesn't use either the Load or the LoadFrom context.
            // LoadFile() has a catch. Since it doesn't use a binding context, its dependencies aren't automatically found in its directory.

            var assembly = Assembly.Load(assemblyString);
            var types    = assembly.GetTypes(); //AnotherService and MyService

            // TODO: Find first type that has DoSomething attribute and implements IDoSomething.
            // TODO: Create an instance of this type.
            Type type = types.First(x =>
                                    x.GetCustomAttributes(typeof(DoSomethingAttribute)) != null && x.GetInterface("IDoSomething") != null);

            var instanceType = Activator.CreateInstance(type);

            IDoSomething doSomethingService = null; // TODO Save instance to variable.

            doSomethingService = (IDoSomething)instanceType;
            return(doSomethingService.DoSomething(data));
        }
Exemplo n.º 12
0
        private static void Method1(Input input)
        {
            var appDomainSetup = new AppDomainSetup
            {
                ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
                PrivateBinPath  = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MyDomain")
            };

            // TODO Create a new domain with name MyDomain and appDomainSetup.
            AppDomain domain = null;

            // TODO Subscribe to the event that will be raised when assembly is loaded.
            {
                // TODO Set a full name of an assembly that is loaded.
                string assemblyFullName = string.Empty;
                Console.WriteLine("Assembly {0} is loaded.", assemblyFullName);
            };

            // TODO Create a new instance of DomainAssemblyLoader in MyDomain and unwrap it by using CreateInstanceAndUnwrap method and specifying class name.
            DomainAssemblyLoader loader = null; // domain.MethodName(Assembly.GetExecutingAssembly().FullName, typeof(ClassName).FullName);

            try
            {
                var assemblyString = "MyLibrary, Version=1.2.3.4, Culture=neutral, PublicKeyToken=f46a87b3d9a80705";

                // NOTE You will get an exception about serialization issues. Try to fix that using SerializableAttribute or MarshalByRefObject.
                IDoSomething doSomething = loader.Load <IDoSomething>(assemblyString);
                var          result      = doSomething.DoSomething(input);

                // TODO Put a breakpoint here and take a look at doSomething and result variables in the run time.

                Console.WriteLine("Method1: {0}", result.Value);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
            }

            // TODO: Unload app. domain.
        }
Exemplo n.º 13
0
        // More details: http://stackoverflow.com/questions/1477843/difference-between-loadfile-and-loadfrom-with-net-assemblies
        public Result LoadFrom(string fileName, Input data)
        {
            var assembly = Assembly.LoadFrom(fileName);
            var types    = assembly.GetTypes();

            // TODO: Find first type that has DoSomething attribute and implements IDoSomething.

            var  typesWithAttribute = types.Where(t => t.GetCustomAttributes(typeof(DoSomethingAttribute), true).Length > 0);
            var  typeInterface      = typeof(IDoSomething);
            Type type = typesWithAttribute.First(p => typeInterface.IsAssignableFrom(p));

            // TODO: Create an instance of this type.

            var instance = Activator.CreateInstance(type);

            IDoSomething doSomethingService = null;

            // TODO Save instance to variable.

            doSomethingService = instance as IDoSomething;

            return(doSomethingService?.DoSomething(data));
        }
Exemplo n.º 14
0
        // Before making this call make sure that MyInterface assembly is signed with mykey.snk file. See Signing tab in MyInterface project properties editor.
        // Usage:
        // result = loader.Load("MyLibrary, Version=1.2.3.4, Culture=neutral, PublicKeyToken=f46a87b3d9a80705", input)
        public Result Load(string assemblyString, Input data)
        {
            // LoadFile() doesn't bind through Fusion at all - the loader just goes ahead and loads exactly what the caller requested.
            // It doesn't use either the Load or the LoadFrom context.
            // LoadFile() has a catch. Since it doesn't use a binding context, its dependencies aren't automatically found in its directory.

            var assembly = Assembly.Load(assemblyString);
            var types    = assembly.GetTypes();

            // TODO: Find first type that has DoSomething attribute and implements IDoSomething.
            // TODO: Create an instance of this type.

            IDoSomething doSomethingService = null; // TODO Save instance to variable.

            Type type = types.FirstOrDefault(t => t.GetCustomAttribute <DoSomethingAttribute>() != null && typeof(IDoSomething).IsAssignableFrom(t));

            if (!ReferenceEquals(type, null))
            {
                doSomethingService = (IDoSomething)Activator.CreateInstance(type);
            }

            return(doSomethingService.DoSomething(data));
        }
Exemplo n.º 15
0
        // Before making this call make sure that MyInterface assembly is signed with mykey.snk file. See Signing tab in MyInterface project properties editor.
        // Usage:
        // result = loader.Load("MyLibrary, Version=1.2.3.4, Culture=neutral, PublicKeyToken=f46a87b3d9a80705", input)
        public Result Load(string assemblyString, Input data)
        {
            // LoadFile() doesn't bind through Fusion at all - the loader just goes ahead and loads exactly what the caller requested.
            // It doesn't use either the Load or the LoadFrom context.
            // LoadFile() has a catch. Since it doesn't use a binding context, its dependencies aren't automatically found in its directory.

            Console.WriteLine("Start Load(string assemblyString, Input data).");

            var assembly = Assembly.Load(assemblyString);
            var types    = assembly.GetTypes();

            Console.WriteLine("All types which were loaded from assembly:");
            foreach (var t in types)
            {
                Console.WriteLine(t);
            }

            // TODO: Find first type that has DoSomething attribute and implements IDoSomething.
            // TODO: Create an instance of this type.

            var  typesWithAttribute = types.Where(t => t.GetCustomAttributes(typeof(DoSomethingAttribute), true).Length > 0);
            var  typeInterface      = typeof(IDoSomething);
            Type type = typesWithAttribute.First(p => typeInterface.IsAssignableFrom(p));

            Console.WriteLine("Seeking type: " + type + "\nGet type this object: " + type.Name);

            var instance = Activator.CreateInstance(type);

            IDoSomething doSomethingService = null;

            // TODO Save instance to variable.

            doSomethingService = instance as IDoSomething;

            return(doSomethingService.DoSomething(data));
        }
 public AClassThatNeedsToDoSomething(IDoSomething doSomething)
 {
     this.doSomething = doSomething;
 }
 public RecieverThatDoesSomething(IDoSomething doSomething)
 {
     this.doSomething = doSomething;
 }
 public Increment(IDoSomething something)
 {
     this.something = something;
 }
 static InspireMe()
 {
     Instance = new InspireMe();
 }
 static InsultMe()
 {
     Instance = new InsultMe();
 }
 public static void Handle(Message2 message, IRepository repository, SomethingContext c, IDoSomething doSomething)
 {
     logger.Info("Handle {0}={1}", nameof(Message2), message);
 }
 static DoSomething()
 {
     Instance = new DoSomething();
 }
 public DoublingDecorator(IDoSomething something)
 {
     this.something = something;
 }
 protected IDoSomethingTestBase(IDoSomething instanceUnderTest){
   InstanceUnderTest = instanceUnderTest;
 }
Exemplo n.º 25
0
 public DerivedClass(Doer doer, IDoSomething anotherDoer) : base(doer, anotherDoer)
 {
 }
 public static void DoThis(this IDoSomething x)
 {
     x.DoSomething(123);     //default value
 }
Exemplo n.º 27
0
 public void DoLibraryStuff(IDoSomething iDoSomething)
 {
     iDoSomething.DoSomething("info"); ...
 public static void Handle(Message2 message, IRepository repository, SomethingContext c, IDoSomething doSomething)
 {
     logger.Info("Handle {0}={1}", nameof(Message2), message);
 }
 public LoggedDoSomething(IDoSomething innerDoSomething)
 {
     this._innerDoSomething = innerDoSomething;
 }
Exemplo n.º 30
0
 public ClassC(IDoSomething doSomething)
 {
     _doSomething = doSomething;
 }
Exemplo n.º 31
0
 public SomeComponent(IDoSomething someOtherComponent)
 {
     _someOtherComponent = someOtherComponent ?? throw new ArgumentNullException(nameof(someOtherComponent));
 }
Exemplo n.º 32
0
 public ConditionallyDoSomething(IDoSomething wrapped)
 {
     _wrapped = wrapped;
 }
Exemplo n.º 33
0
 public static int DoSomething(this IDoSomething tThis)
 {
     return(8);
 }
Exemplo n.º 34
0
 public BaseClass(Doer doer, IDoSomething anotherDoer)
 {
 }
 public LoggedDoSomething(IDoSomething innerDoSomething)
 {
     this._innerDoSomething = innerDoSomething;
 }
 public AClassThatNeedsToDoSomething(IDoSomething doSomething)
 {
     this.doSomething = doSomething;
 }
Exemplo n.º 37
0
 public OneClass(IDoSomething doSomething)
 {
     _doSomething = doSomething;
 }
Exemplo n.º 38
0
 public TestHostArr(IDoSomething[] plugins)
 {
     _plugins = plugins;
 }
 public CountAndAdd(IDoSomething something)
 {
     this.something = something;
 }
Exemplo n.º 40
0
 public ClassToBeTested(IDoSomething doSomething)
 {
     this.doSomething = doSomething;
 }
 public ClassToBeTested(IDoSomething doSomething)
 {
     this.doSomething = doSomething;
 }