示例#1
0
        static void Main(string[] args)
        {
            var privateClass = new PrivateClass();

            //Fields
            Console.WriteLine(Solution.GetFieldValue <string>(privateClass, "privateField"));

            Solution.SetFieldValue(privateClass, "privateField", "I took over.");

            Console.WriteLine(Solution.GetFieldValue <string>(privateClass, "privateField"));


            //Properties
            Console.WriteLine(Solution.GetPropertyValue <string>(privateClass, "PrivateProp"));

            Solution.SetPropertyValue(privateClass, "PrivateProp", "I took over.");

            Console.WriteLine(Solution.GetPropertyValue <string>(privateClass, "PrivateProp"));

            //Methods
            Console.WriteLine(Solution.InvokeMethod(privateClass, "PrivateMethod", "1", "2"));

            Console.WriteLine(Solution.InvokeStaticMethod(typeof(PrivateClass), "PrivateStaticMethod", "1", "2"));

            Console.ReadLine();

            //Getting attributes
            Console.WriteLine($"I was in the {BuildingType.Pool.GetDescription()}.");

            Console.ReadLine();

            //Real stuff
            var demanding = new DemandingClass();

            //this fails horribly
            //demanding.Perform();

            Solution.SatisfyTheClass(demanding);

            //this works
            demanding.Perform();
            Console.ReadLine();
        }
示例#2
0
        public static void SatisfyTheClass(DemandingClass demandingClass)
        {
            var exportedType =
                AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(assembly => assembly.GetTypes())
                .FirstOrDefault(type => type.GetCustomAttribute(typeof(ExportAttribute)) != null);

            if (exportedType == null)
            {
                throw new InvalidOperationException("Expected type with export atribute does not exist in the app.");
            }

            var demandedInstance = Activator.CreateInstance(exportedType);

            if (demandedInstance == null)
            {
                throw new InvalidOperationException($"Could not create instance of type {exportedType.FullName}. Missing parameterless constructor maybe?");
            }

            SetFieldValue(demandingClass, "demandedClass", demandedInstance);
        }