예제 #1
0
        public static void LoadReferences(BaseModuleWeaver weaver)
        {
            _weaver = weaver;

            DebugWriteLineMethod = weaver.FindType("System.Diagnostics.Debug").Method("WriteLine");
            StringFormatMethod   = weaver.FindType("System.String").Method("Format", new string[] { "String", "Object[]" });
            SystemTypeGetTypeFromHandleMethod = weaver.FindType("Type").Method("GetTypeFromHandle");

            var typeDictionary = weaver.FindType("Dictionary`2");
            var genericArg     = new TypeReference[] { weaver.ModuleDefinition.TypeSystem.String, weaver.ModuleDefinition.TypeSystem.Object };
            var genericDic     = typeDictionary.MakeGenericInstanceType(genericArg);

            DictionaryConstructor = genericDic.Resolve().GetConstructors().FirstOrDefault();
            DictionaryAddMethod   = genericDic.Resolve().Method("Add");

            //make ctor method has generic parms, cause Resolve() will remove generic parameters all
            DictionaryConstructor.DeclaringType = DictionaryConstructor.DeclaringType.MakeGenericInstanceType(genericArg);
            DictionaryAddMethod.DeclaringType   = DictionaryAddMethod.DeclaringType.MakeGenericInstanceType(genericArg);

#if DEBUG
            CacheAssembly = weaver.ResolveAssembly("Cache");
#else
            //load Cache reference
            var references = SplitUpReferences(weaver);
            AssemblyResolver = new AssemblyResolver(references);
            CacheAssembly    = AssemblyResolver.Resolve("Cache");
#endif
        }
예제 #2
0
    public void FindReferences(BaseModuleWeaver moduleWeaver)
    {
        var moduleDefinition = moduleWeaver.ModuleDefinition;

        SystemType = moduleWeaver.FindType("System.Type");
        DebuggerBrowsableStateType = moduleWeaver.FindType("System.Diagnostics.DebuggerBrowsableState");

        var listType = moduleWeaver.FindType("System.Collections.Generic.List`1");

        ListCtor    = moduleDefinition.ImportReference(listType.Methods.First(m => m.IsConstructor && m.Parameters.Count == 1 && m.Parameters[0].ParameterType.Name.StartsWith("IEnumerable")));
        ListToArray = moduleDefinition.ImportReference(listType.Methods.First(m => m.Name == "ToArray"));

        var debuggerBrowsableAttribute = moduleWeaver.FindType("System.Diagnostics.DebuggerBrowsableAttribute");

        DebuggerBrowsableAttributeCtor = moduleDefinition.ImportReference(debuggerBrowsableAttribute.Methods.First(x => x.IsConstructor));

        var debuggerDisplayAttribute = moduleWeaver.FindType("System.Diagnostics.DebuggerDisplayAttribute");

        DebuggerDisplayAttributeCtor = moduleDefinition.ImportReference(debuggerDisplayAttribute.Methods.First(x => x.IsConstructor));

        var debuggerTypeProxyAttribute = moduleWeaver.FindType("System.Diagnostics.DebuggerTypeProxyAttribute");

        DebuggerTypeProxyAttributeCtor = moduleDefinition.ImportReference(debuggerTypeProxyAttribute.Methods.First(x => x.IsConstructor &&
                                                                                                                   x.Parameters[0].ParameterType.FullName == "System.Type"));

        var compilerGeneratedAttribute = moduleWeaver.FindType("System.Runtime.CompilerServices.CompilerGeneratedAttribute");

        CompilerGeneratedAttributeCtor = moduleDefinition.ImportReference(compilerGeneratedAttribute.Methods.First(x => x.IsConstructor));

        StringFormat = moduleDefinition.ImportReference(moduleWeaver.FindType("System.String").Methods
                                                        .First(m => m.Name == "Format" && m.Parameters.Count == 2 && m.Parameters[0].ParameterType.FullName == "System.String" && m.Parameters[1].ParameterType.FullName == "System.Object[]"));
    }
        private static MethodReference GetMemberInfoNamePropertyGetMethod(BaseModuleWeaver weaver)
        {
            TypeDefinition     memberInfoType         = weaver.FindType(typeof(MemberInfo).FullName);
            PropertyDefinition memberInfoNameProperty = memberInfoType.Properties.Single(property => property.Name == nameof(MemberInfo.Name));

            return(weaver.ModuleDefinition.ImportReference(memberInfoNameProperty.GetMethod));
        }
예제 #4
0
        private static MethodReference GetHasValueGetMethod(BaseModuleWeaver weaver)
        {
            TypeDefinition     nullableType     = weaver.FindType(typeof(Nullable <>).FullName);
            PropertyDefinition hasValueProperty = nullableType.Properties.Single(property => property.Name == nameof(Nullable <int> .HasValue));

            return(weaver.ModuleDefinition.ImportReference(hasValueProperty.GetMethod));
        }
예제 #5
0
        private static MethodReference GetIndexerGetMethod(BaseModuleWeaver weaver)
        {
            TypeDefinition     stringType = weaver.FindType(typeof(string).FullName);
            PropertyDefinition indexer    = stringType.Properties.Single(property =>
                                                                         property.Parameters.Count == 1 &&
                                                                         property.Parameters[0].ParameterType.FullName == typeof(int).FullName);

            return(weaver.ModuleDefinition.ImportReference(indexer.GetMethod));
        }
예제 #6
0
        private static MethodReference GetToStringMethod(BaseModuleWeaver weaver)
        {
            TypeDefinition   objectType           = weaver.FindType(typeof(object).FullName);
            MethodDefinition objectToStringMethod = objectType.Methods.Single(method =>
                                                                              method.IsStatic == false &&
                                                                              method.Name == nameof(object.ToString) &&
                                                                              method.Parameters.Count == 0);

            return(weaver.ModuleDefinition.ImportReference(objectToStringMethod));
        }
예제 #7
0
        private static MethodReference GetArgumentNullExceptionWithMessageConstructor(BaseModuleWeaver weaver)
        {
            TypeDefinition   argumentNullExceptionType = weaver.FindType(typeof(ArgumentNullException).FullName);
            MethodDefinition argumentNullExceptionWithMessageConstructor = argumentNullExceptionType.Methods.Single(method =>
                                                                                                                    method.IsConstructor &&
                                                                                                                    method.Parameters.Count == 2 &&
                                                                                                                    method.Parameters[0].ParameterType.FullName == typeof(string).FullName &&
                                                                                                                    method.Parameters[1].ParameterType.FullName == typeof(string).FullName);

            return(weaver.ModuleDefinition.ImportReference(argumentNullExceptionWithMessageConstructor));
        }
        private static MethodReference GetGetTypeFromHandleMethod(BaseModuleWeaver weaver)
        {
            TypeDefinition   typeType = weaver.FindType(typeof(Type).FullName);
            MethodDefinition getTypeFromHandleMethod = typeType.Methods.Single(method =>
                                                                               method.IsStatic &&
                                                                               method.Name == nameof(Type.GetTypeFromHandle) &&
                                                                               method.Parameters.Count == 1 &&
                                                                               method.Parameters[0].ParameterType.FullName == typeof(RuntimeTypeHandle).FullName);

            return(weaver.ModuleDefinition.ImportReference(getTypeFromHandleMethod));
        }
예제 #9
0
        private static MethodReference GetConcatThreeStringsMethod(BaseModuleWeaver weaver)
        {
            TypeDefinition   stringType = weaver.FindType(typeof(string).FullName);
            MethodDefinition concatThreeStringsMethod = stringType.Methods.Single(method =>
                                                                                  method.IsStatic &&
                                                                                  method.Name == nameof(string.Concat) &&
                                                                                  method.Parameters.Count == 3 &&
                                                                                  method.Parameters[0].ParameterType.FullName == typeof(string).FullName &&
                                                                                  method.Parameters[1].ParameterType.FullName == typeof(string).FullName &&
                                                                                  method.Parameters[2].ParameterType.FullName == typeof(string).FullName);

            return(weaver.ModuleDefinition.ImportReference(concatThreeStringsMethod));
        }
예제 #10
0
 private static TypeReference GetNullableType(BaseModuleWeaver weaver)
 {
     return(weaver.ModuleDefinition.ImportReference(weaver.FindType(typeof(Nullable <>).FullName)));
 }