private WeakEventHandlerWeaver(ModuleDefinition moduleDefinition, ITypeSystem typeSystem, ILogger logger, TypeReference makeWeakAttributeReference)
        {
            _moduleDefinition = moduleDefinition;
            _typeSystem       = typeSystem;
            _logger           = logger;

            var makeWeakAttribute = makeWeakAttributeReference.Resolve();

            var helperTypes = makeWeakAttribute.Module.Types;

            _codeImporter = new CodeImporter(moduleDefinition)
            {
                NamespaceDecorator = value => "<>" + value
            };

            _weakAdapterType      = _codeImporter.Import(helperTypes.Single(t => t.Name == "WeakEventHandlerFodyWeakEventAdapter`4"));
            _eventTargetInterface = _codeImporter.Import(helperTypes.Single(t => t.Name == "IWeakEventHandlerFodyWeakEventTarget"));

            _weakAdapterConstructor = _weakAdapterType.GetConstructors().Single(ctor => ctor.Parameters.Count == 4);
            _generatedCodeAttribute = _weakAdapterType.CustomAttributes.Single(attr => attr.AttributeType.Name == nameof(GeneratedCodeAttribute));

            var weakAdapterMethods = _weakAdapterType.GetMethods().ToList();

            _weakAdapterSubscribeMethod   = weakAdapterMethods.Single(method => method.Name == "Subscribe");
            _weakAdapterUnsubscribeMethod = weakAdapterMethods.Single(method => method.Name == "Unsubscribe");
            _weakAdapterReleaseMethod     = weakAdapterMethods.Single(method => method.Name == "Release");

            _action2Type        = typeSystem.ImportType <Action <object, EventArgs> >();
            _action2Constructor = typeSystem.ImportMethod <Action <object, EventArgs>, object, IntPtr>(".ctor");
            _action3Type        = typeSystem.ImportType <Action <Type, object, EventArgs> >();
            _action3Constructor = typeSystem.ImportMethod <Action <Type, object, EventArgs>, object, IntPtr>(".ctor");
        }
Пример #2
0
        public void OnGenericTypeTest1()
        {
            var module   = ModuleHelper.LoadModule <EmptyClass>();
            var importer = new CodeImporter(module)
            {
                CompactMode = false
            };

            var type   = importer.Import <SimpleSampleClass>();
            var method = importer.ImportMethod(() => default(SimpleGenericClass <T>) !.Method(default));
Пример #3
0
        public void ComplexTypesTest(int numberOfTypes, params Type[] types)
        {
            var module = ModuleHelper.LoadModule <EmptyClass>();

            var governingType = types.First();

            var target = new CodeImporter(module)
            {
                // else IL comparison will fail:
                HideImportedTypes = false,
                CompactMode       = false
            };

            foreach (var type in types)
            {
                target.Import(type);
            }

            var tempPath = TestHelper.TempPath;

            var targetAssemblyPath = Path.Combine(tempPath, "TargetAssembly2.dll");

            module.Write(targetAssemblyPath);

            var sourceAssemblyPath = Path.Combine(tempPath, "SourceAssembly2.dll");

            var sourceModule = ModuleHelper.LoadModule(new Uri(governingType.Assembly.CodeBase).LocalPath);

            sourceModule.Assembly.Name.Name = "SourceAssembly";
            sourceModule.Write(sourceAssemblyPath);

            var importedTypes = target.ListImportedTypes();

            if (importedTypes.Keys.Select(t => t.FullName).Contains("TomsToolbox.Core.NetStandardExtensions"))
            {
                numberOfTypes += 1;
            }

            foreach (var type in importedTypes)
            {
                _testOutputHelper.WriteLine(type.Key.FullName);
            }


            Assert.Equal(numberOfTypes, importedTypes.Count);

            TestHelper.VerifyTypes(importedTypes, target.ListImportedModules(), targetAssemblyPath);

            Assert.True(TestHelper.PEVerify.Verify(targetAssemblyPath, _testOutputHelper));
        }
Пример #4
0
        public void SimpleTypesTest(int numberOfTypes, ModuleResolver moduleResolver, params Type[] types)
        {
            var module = ModuleHelper.LoadModule <EmptyClass>();

            var moduleResolverInstance = moduleResolver == ModuleResolver.AssemblyModuleResolver
                ? (IModuleResolver) new AssemblyModuleResolver(typeof(TomsToolbox.Core.AssemblyExtensions).Assembly, typeof(TomsToolbox.Core.DefaultValue).Assembly)
                : new LocalReferenceModuleResolver();

            var targetAssemblyPath = Path.Combine(TestHelper.TempPath, "TargetAssembly1.dll");

            var target = new CodeImporter(module)
            {
                ModuleResolver    = moduleResolverInstance,
                HideImportedTypes = false,
                CompactMode       = false
            };

            foreach (var type in types)
            {
                target.Import(type);
            }

            module.Write(targetAssemblyPath);

            var importedTypes = target.ListImportedTypes();

            foreach (var type in importedTypes)
            {
                _testOutputHelper.WriteLine(type.Key.FullName);
            }

            var importedModules = target.ListImportedModules();

            Assert.True(TestHelper.PEVerify.Verify(targetAssemblyPath, _testOutputHelper));
            Assert.Equal(numberOfTypes, importedTypes.Count);

            TestHelper.VerifyTypes(importedTypes, importedModules, targetAssemblyPath);
        }
Пример #5
0
        private static void Execute([NotNull] ModuleDefinition moduleDefinition, [NotNull] ILogger logger, [NotNull] string addInDirectoryPath, [NotNull] string assemblyFilePath, [NotNull] IList <string> referenceCopyLocalPaths)
        {
            var entryPoint = moduleDefinition.EntryPoint;

            if (entryPoint == null)
            {
                logger.LogError("No entry point found in target module.");
                return;
            }

            TypeDefinition splashScreenControl;

            try
            {
                splashScreenControl = moduleDefinition.Types.Single(HasSplashScreenAttribute);
            }
            catch (Exception ex)
            {
                logger.LogError("No single class with the [SplashScreen] attribute found: " + ex.Message);
                return;
            }

            byte[] bitmapData;

            try
            {
                bitmapData = BitmapGenerator.Generate(addInDirectoryPath, assemblyFilePath, splashScreenControl.FullName, referenceCopyLocalPaths);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return;
            }

            var splashScreenControlBamlResourceName = splashScreenControl.Name.ToLowerInvariant() + ".baml";

            ResourceHelper.UpdateResources(moduleDefinition, SplashResourceName, bitmapData, splashScreenControlBamlResourceName);

            moduleDefinition.Types.Remove(splashScreenControl);

            var attribute = GetSplashScreenAttribute(splashScreenControl);

            var minimumVisibilityDuration = attribute.GetPropertyValue(MinimumVisibilityDurationPropertyName, 4.0);
            var fadeoutDuration           = attribute.GetPropertyValue(FadeoutDurationPropertyName, 1.0);

            var referencedModule = attribute.AttributeType.Resolve().Module;

            var adapterType = referencedModule.Types.Single(type => type.Name == SplashScreenAdapterTypeName);

            var importer = new CodeImporter(moduleDefinition);

            adapterType = importer.Import(adapterType);

            importer.ILMerge();

            var adapterTypeConstructor = adapterType.GetConstructors().Single(ctor => ctor.Parameters.Count == 3);

            entryPoint.Body.Instructions.InsertRange(0,
                                                     Instruction.Create(OpCodes.Ldstr, SplashResourceName),
                                                     Instruction.Create(OpCodes.Ldc_R8, minimumVisibilityDuration),
                                                     Instruction.Create(OpCodes.Ldc_R8, fadeoutDuration),
                                                     Instruction.Create(OpCodes.Newobj, moduleDefinition.ImportReference(adapterTypeConstructor)),
                                                     Instruction.Create(OpCodes.Pop)
                                                     );
        }