protected ModuleProcessorBase([NotNull] ModuleWeaver moduleWeaver)
        {
            if (moduleWeaver == null)
            {
                throw new ArgumentNullException("moduleWeaver");
            }

            _moduleWeaver = moduleWeaver;
            _assets = _moduleWeaver.Assets;
            _moduleDefinition = _moduleWeaver.ModuleDefinition;
            _settings = _moduleWeaver.Settings;
        }
 protected TypeProcessorBase(TypeDefinition type, ModuleWeaver moduleWeaver)
 {
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     if (moduleWeaver == null)
     {
         throw new ArgumentNullException("moduleWeaver");
     }
     _type = type;
     _moduleWeaver = moduleWeaver;
     _assets = _moduleWeaver.Assets;
 }
Пример #3
0
        public Types([NotNull] ModuleWeaver moduleWeaver)
        {            
            if (moduleWeaver == null)
            {
                throw new ArgumentNullException("moduleWeaver");
            }

            _moduleWeaver = moduleWeaver;
            var moduleDefinition = _moduleDefinition = moduleWeaver.ModuleDefinition;

            _string = moduleDefinition.TypeSystem.String;
            _void = moduleDefinition.TypeSystem.Void;
            _object = moduleDefinition.TypeSystem.Object;
            _boolean = moduleDefinition.TypeSystem.Boolean;

            var targetFramework = moduleDefinition.Assembly.GetTargetFramework();
            var assemblyResolver = ModuleDefinition.AssemblyResolver;
            var msCoreTypes = GetMscorlibTypes(targetFramework);

            var systemTypes = GetSystemTypes(targetFramework);            

            var objectDefinition = msCoreTypes.FirstOrDefault(x => x.Name == "Object")
                ?? systemTypes.FirstOrDefault(x => x.Name == "Object");
            if (objectDefinition == null)
            {
                ExecuteWinRT();
                return;
            }
            _objectDef = objectDefinition;
            _eventHandlerDef = msCoreTypes.First(x => x.Name == "EventHandler");
            _eventHandler = ModuleDefinition.Import(_eventHandlerDef);
            _delegateDef = msCoreTypes.First(x => x.Name == "Delegate");
            _delegate = ModuleDefinition.Import(_delegateDef);

            _interlockedDef = msCoreTypes.First(x => x.FullName == "System.Threading.Interlocked");
            _interlocked = ModuleDefinition.Import(_interlockedDef);

            var actionDefinition = msCoreTypes.FirstOrDefault(x => x.Name == "Action");
            if (actionDefinition == null)
            {
                actionDefinition = systemTypes.FirstOrDefault(x => x.Name == "Action");
            }
            var systemCoreDefinition = GetSystemCoreDefinition();
            if (actionDefinition == null)
            {
                actionDefinition = systemCoreDefinition.MainModule.Types.First(x => x.Name == "Action");
            }
            _actionDef = actionDefinition;
            _action = ModuleDefinition.Import(actionDefinition);

            actionDefinition = msCoreTypes.FirstOrDefault(x => x.Name == "Action`1");
            if (actionDefinition == null)
            {
                actionDefinition = systemTypes.FirstOrDefault(x => x.Name == "Action`1");
            }
            if (actionDefinition == null)
            {
                actionDefinition = systemCoreDefinition.MainModule.Types.First(x => x.Name == "Action`1");
            }
            _actionOfTDef = actionDefinition;
            _actionOfT = ModuleDefinition.Import(actionDefinition);

            var funcFilter = new Func<TypeDefinition, bool>(x => x.Name.StartsWith("Func") && x.HasGenericParameters && x.GenericParameters.Count == 1);
            var funcDefinition = msCoreTypes.FirstOrDefault(funcFilter);
            if (funcDefinition == null)
            {
                funcDefinition = systemTypes.FirstOrDefault(funcFilter);
            }
            if (funcDefinition == null)
            {
                funcDefinition = systemCoreDefinition.MainModule.Types.First(funcFilter);
            }
            _funcOfTDef = funcDefinition;
            _funcOfT = ModuleDefinition.Import(funcDefinition);

            var predicateFilter = new Func<TypeDefinition, bool>(x => x.Name.StartsWith("Predicate") && x.HasGenericParameters && x.GenericParameters.Count == 1);
            var predicateDefinition = msCoreTypes.FirstOrDefault(predicateFilter);
            if (predicateDefinition == null)
            {
                predicateDefinition = systemTypes.FirstOrDefault(predicateFilter);
            }
            if (predicateDefinition == null)
            {
                predicateDefinition = systemCoreDefinition.MainModule.Types.First(predicateFilter);
            }
            _predicateOfTDef = predicateDefinition;
            _predicateOfT = ModuleDefinition.Import(predicateDefinition);

            var argumentNullException = msCoreTypes.FirstOrDefault(x => x.Name == "ArgumentNullException");
            if (argumentNullException == null)
            {
                argumentNullException = systemTypes.First(x => x.Name == "ArgumentNullException");
            }
            _argumentNullExceptionDef = argumentNullException;
            _argumentNullException = ModuleDefinition.Import(argumentNullException);

            var commandPrimaryAssemblyDef = GetPrimaryICommandSearchLocation(targetFramework);
            var presentationCoreTypes = commandPrimaryAssemblyDef.MainModule.Types;
            var iCommandDefinition = presentationCoreTypes.FirstOrDefault(x => x.Name == "ICommand");
            if (iCommandDefinition == null)
            {
                iCommandDefinition = systemTypes.FirstOrDefault(x => x.Name == "ICommand");
            }
            _iCommandDef = iCommandDefinition;
            _iCommand = ModuleDefinition.Import(iCommandDefinition);
            if (_iCommand == null)
            {
                const string message = "Could not find type System.Windows.Input.ICommand.";
                throw new WeavingException(message);
            }
            var commandManagerDefinition = presentationCoreTypes.FirstOrDefault(x => x.Name == "CommandManager");
            if (commandManagerDefinition == null)
            {
                commandManagerDefinition = systemTypes.FirstOrDefault(x => x.Name == "CommandManager");
            }
            _commandManagerDef = commandManagerDefinition;
            if (commandManagerDefinition != null)
            {
                _commandManager = ModuleDefinition.Import(commandManagerDefinition);                       
            }            
        }
Пример #4
0
        public Assets(ModuleWeaver moduleWeaver)
        {
            if (moduleWeaver == null)
            {
                throw new ArgumentNullException(nameof(moduleWeaver));
            }

            Commands         = new ConcurrentDictionary <string, CommandData>();
            ModuleDefinition = moduleWeaver.ModuleDefinition;
            var settings = moduleWeaver.Settings;

            _allClasses = new Lazy <List <TypeDefinition> >(() => settings.GetAllTypes(moduleWeaver).ToList());
            var types = new Types(moduleWeaver);

            TypeReferences  = types;
            TypeDefinitions = types;

            var constructorDefinition = TypeDefinitions.Object.Methods.First(x => x.IsConstructor);

            ObjectConstructorReference = ModuleDefinition.ImportReference(constructorDefinition);
            var actionConstructor = TypeDefinitions.Action.Methods.First(x => x.IsConstructor);

            ActionConstructorReference = ModuleDefinition.ImportReference(actionConstructor);
            var actionOfTConstructor = TypeDefinitions.ActionOfT.GetConstructors().First();

            ActionOfTConstructorReference = ModuleDefinition.ImportReference(actionOfTConstructor);
            var actionOfTInvokerDefinition = TypeDefinitions.ActionOfT.Methods.First(x => x.Name == "Invoke");

            ActionOfTInvokeReference = ModuleDefinition.ImportReference(actionOfTInvokerDefinition);
            var funcConstructor = TypeDefinitions.FuncOfT.Resolve().Methods.First(m => m.IsConstructor && m.Parameters.Count == 2);

            FuncOfBoolConstructorReference = ModuleDefinition.ImportReference(funcConstructor).MakeHostInstanceGeneric(TypeReferences.Boolean);
            var predicateOfTConstructor = TypeDefinitions.PredicateOfT.GetConstructors().First();

            PredicateOfTConstructorReference = ModuleDefinition.ImportReference(predicateOfTConstructor);
            var predicateOfTInvokerDefinition = TypeDefinitions.PredicateOfT.Methods.First(x => x.Name == "Invoke");

            PredicateOfTInvokeReference = ModuleDefinition.ImportReference(predicateOfTInvokerDefinition);
            var delegateCombineDefinition = TypeDefinitions.Delegate.Methods.First(x => x.Name == "Combine" && x.Parameters.Count == 2);

            DelegateCombineMethodReference = ModuleDefinition.ImportReference(delegateCombineDefinition);
            var delegateRemoveDefinition = TypeDefinitions.Delegate.Methods.First(x => x.Name == "Remove" && x.Parameters.Count == 2);

            DelegateRemoveMethodReference = ModuleDefinition.ImportReference(delegateRemoveDefinition);

            var interlockedCompareExchangeMethodDefinition = TypeDefinitions.Interlocked.Methods.First(
                x => x.Name == "CompareExchange" &&
                x.Parameters.Count == 3 &&
                x.HasGenericParameters
                );

            InterlockedCompareExchangeOfT             = ModuleDefinition.ImportReference(interlockedCompareExchangeMethodDefinition);
            _interlockedCompareExchangeOfEventHandler = new GenericInstanceMethod(InterlockedCompareExchangeOfT);
            _interlockedCompareExchangeOfEventHandler.GenericArguments.Add(TypeReferences.EventHandler);
            //_interlockedCompareExchangeOfEventHandler =
            if (TypeDefinitions.CommandManager != null)
            {
                var requeryEvent = TypeDefinitions.CommandManager.Resolve().Events.Single(evt => evt.Name == "RequerySuggested");
                CommandManagerAddRequerySuggestedMethodReference    = ModuleDefinition.ImportReference(requeryEvent.AddMethod);
                CommandManagerRemoveRequerySuggestedMethodReference = ModuleDefinition.ImportReference(requeryEvent.RemoveMethod);
            }
            CommandImplementationConstructors = GetCommandImplementationConstructors();

            constructorDefinition =
                TypeDefinitions.ArgumentNullException.Methods.Single(
                    x => x.IsConstructor && x.HasParameters && x.Parameters.Count == 1);
            ArgumentNullExceptionConstructorReference = ModuleDefinition.ImportReference(constructorDefinition);
        }
Пример #5
0
 public virtual IEnumerable <TypeDefinition> GetTypesToProcess(ModuleWeaver moduleWeaver)
 {
     return(GetAllClasses(moduleWeaver));
 }
Пример #6
0
 public virtual IEnumerable <TypeDefinition> GetAllClasses(ModuleWeaver moduleWeaver)
 {
     return(GetAllTypes(moduleWeaver).Where(x => x.IsClass));
 }
Пример #7
0
 public virtual IEnumerable <TypeDefinition> GetAllTypes(ModuleWeaver moduleWeaver)
 {
     return(moduleWeaver.ModuleDefinition.GetTypes());
 }
Пример #8
0
 public CommandInjectionTypeProcessor(TypeDefinition type, ModuleWeaver moduleWeaver) : base(type, moduleWeaver)
 {
     _commands = Assets.Commands.Values.Where(cmd => cmd.DeclaringType.FullName == type.FullName).ToList();
 }
Пример #9
0
 public CommandAttributeScanner([NotNull] ModuleWeaver moduleWeaver) : base(moduleWeaver)
 {
 }
Пример #10
0
        public Types(ModuleWeaver moduleWeaver)
        {
            _moduleWeaver = moduleWeaver ?? throw new ArgumentNullException(nameof(moduleWeaver));
            var moduleDefinition = ModuleDefinition = moduleWeaver.ModuleDefinition;

            _string  = moduleDefinition.TypeSystem.String;
            _void    = moduleDefinition.TypeSystem.Void;
            _object  = moduleDefinition.TypeSystem.Object;
            _boolean = moduleDefinition.TypeSystem.Boolean;

            var targetFramework  = moduleDefinition.Assembly.GetTargetFramework();
            var assemblyResolver = ModuleDefinition.AssemblyResolver;
            var msCoreTypes      = GetMscorlibTypes(targetFramework);

            var systemTypes = GetSystemTypes(targetFramework);

            var objectDefinition = msCoreTypes.FirstOrDefault(x => x.Name == "Object")
                                   ?? systemTypes.FirstOrDefault(x => x.Name == "Object");

            if (objectDefinition == null)
            {
                ExecuteWinRT();
                return;
            }
            _objectDef       = objectDefinition;
            _eventHandlerDef = msCoreTypes.First(x => x.Name == "EventHandler");
            _eventHandler    = ModuleDefinition.ImportReference(_eventHandlerDef);
            _delegateDef     = msCoreTypes.First(x => x.Name == "Delegate");
            _delegate        = ModuleDefinition.ImportReference(_delegateDef);

            _interlockedDef = msCoreTypes.First(x => x.FullName == "System.Threading.Interlocked");
            _interlocked    = ModuleDefinition.ImportReference(_interlockedDef);

            var actionDefinition = msCoreTypes.FirstOrDefault(x => x.Name == "Action");

            if (actionDefinition == null)
            {
                actionDefinition = systemTypes.FirstOrDefault(x => x.Name == "Action");
            }
            var systemCoreDefinition = GetSystemCoreDefinition();

            if (actionDefinition == null)
            {
                actionDefinition = systemCoreDefinition.MainModule.Types.First(x => x.Name == "Action");
            }
            _actionDef = actionDefinition;
            _action    = ModuleDefinition.ImportReference(actionDefinition);

            actionDefinition = msCoreTypes.FirstOrDefault(x => x.Name == "Action`1");
            if (actionDefinition == null)
            {
                actionDefinition = systemTypes.FirstOrDefault(x => x.Name == "Action`1");
            }
            if (actionDefinition == null)
            {
                actionDefinition = systemCoreDefinition.MainModule.Types.First(x => x.Name == "Action`1");
            }
            _actionOfTDef = actionDefinition;
            _actionOfT    = ModuleDefinition.ImportReference(actionDefinition);

            var funcFilter     = new Func <TypeDefinition, bool>(x => x.Name.StartsWith("Func") && x.HasGenericParameters && x.GenericParameters.Count == 1);
            var funcDefinition = msCoreTypes.FirstOrDefault(funcFilter);

            if (funcDefinition == null)
            {
                funcDefinition = systemTypes.FirstOrDefault(funcFilter);
            }
            if (funcDefinition == null)
            {
                funcDefinition = systemCoreDefinition.MainModule.Types.First(funcFilter);
            }
            _funcOfTDef = funcDefinition;
            _funcOfT    = ModuleDefinition.ImportReference(funcDefinition);

            var predicateFilter     = new Func <TypeDefinition, bool>(x => x.Name.StartsWith("Predicate") && x.HasGenericParameters && x.GenericParameters.Count == 1);
            var predicateDefinition = msCoreTypes.FirstOrDefault(predicateFilter);

            if (predicateDefinition == null)
            {
                predicateDefinition = systemTypes.FirstOrDefault(predicateFilter);
            }
            if (predicateDefinition == null)
            {
                predicateDefinition = systemCoreDefinition.MainModule.Types.First(predicateFilter);
            }
            _predicateOfTDef = predicateDefinition;
            _predicateOfT    = ModuleDefinition.ImportReference(predicateDefinition);

            var argumentNullException = msCoreTypes.FirstOrDefault(x => x.Name == "ArgumentNullException");

            if (argumentNullException == null)
            {
                argumentNullException = systemTypes.First(x => x.Name == "ArgumentNullException");
            }
            _argumentNullExceptionDef = argumentNullException;
            _argumentNullException    = ModuleDefinition.ImportReference(argumentNullException);

            var commandPrimaryAssemblyDef = GetPrimaryICommandAssemblyDefinition(targetFramework);
            var presentationCoreTypes     = commandPrimaryAssemblyDef.MainModule.Types;
            var iCommandDefinition        = presentationCoreTypes.FirstOrDefault(x => x.Name == "ICommand");

            if (iCommandDefinition == null)
            {
                iCommandDefinition = systemTypes.FirstOrDefault(x => x.Name == "ICommand");
            }
            _iCommandDef = iCommandDefinition;
            _iCommand    = ModuleDefinition.ImportReference(iCommandDefinition);
            if (_iCommand == null)
            {
                const string message = "Could not find type System.Windows.Input.ICommand.";
                throw new Exception(message);
            }
            var commandManagerDefinition = presentationCoreTypes.FirstOrDefault(x => x.Name == "CommandManager");

            if (commandManagerDefinition == null)
            {
                commandManagerDefinition = systemTypes.FirstOrDefault(x => x.Name == "CommandManager");
            }
            _commandManagerDef = commandManagerDefinition;
            if (commandManagerDefinition != null)
            {
                _commandManager = ModuleDefinition.ImportReference(commandManagerDefinition);
            }
        }
 public CommandInjectionTypeProcessor(TypeDefinition type, ModuleWeaver moduleWeaver) : base(type, moduleWeaver)
 {
     _commands = Assets.Commands.Values.Where(cmd => cmd.DeclaringType.FullName == type.FullName).ToList();
 }        
Пример #12
0
 public DelegateCommandClassInjectionProcessor([NotNull] ModuleWeaver moduleWeaver) : base(moduleWeaver)
 {
 }
 public virtual IEnumerable<TypeDefinition> GetTypesToProcess(ModuleWeaver moduleWeaver)
 {
     return GetAllClasses(moduleWeaver);
 }
 public virtual IEnumerable<TypeDefinition> GetAllClasses(ModuleWeaver moduleWeaver)
 {
     return GetAllTypes(moduleWeaver).Where(x => x.IsClass);
 }
 public virtual IEnumerable<TypeDefinition> GetAllTypes(ModuleWeaver moduleWeaver)
 {
     return moduleWeaver.ModuleDefinition.GetTypes();
 }        
 public NestedCommandInjectionTypeProcessor([NotNull] CommandData command,
                                            [NotNull] MethodDefinition initializeMethod, TypeDefinition type, ModuleWeaver moduleWeaver) : base(type, moduleWeaver)
 {
     if (command == null)
     {
         throw new ArgumentNullException("command");
     }
     if (initializeMethod == null)
     {
         throw new ArgumentNullException("initializeMethod");
     }
     _command          = command;
     _initializeMethod = initializeMethod;
 }