예제 #1
0
 private void ImportRuntimeModuleDictionary(ModuleDefinition module, TypeReference tDict)
 {
     var tFuncOfModule = FuncOfT.MakeGenericInstanceType(RuntimeModule);
     var tModuleDict = tDict.MakeGenericInstanceType(Type, tFuncOfModule);
     DictionaryOfTypeToModuleFn = module.Import(tModuleDict);
     DictionaryOfTypeToModuleFn_New =
         module.Import(tModuleDict.Resolve()
                                  .GetDefaultConstructor())
               .MakeHostInstanceGeneric(Type, tFuncOfModule);
     DictionaryOfTypeToModuleFn_Add =
         module.Import(tModuleDict.Resolve().GetMethod("Add")).MakeHostInstanceGeneric(Type, tFuncOfModule);
     DictionaryOfTypeToModuleFn_TryGetValue =
         module.Import(tModuleDict.Resolve().GetMethod("TryGetValue"))
               .MakeHostInstanceGeneric(Type, tFuncOfModule);
 }
예제 #2
0
 private void ImportProviderBindingDictionary(ModuleDefinition module, TypeReference tDict)
 {
     var tFuncOfProviderBinding = FuncOfT5.MakeGenericInstanceType(String, Object, Boolean, String, Binding);
     var tProviderDict = tDict.MakeGenericInstanceType(String, tFuncOfProviderBinding);
     DictionaryOfStringToProviderBindingFn = tProviderDict;
     DictionaryOfStringToProviderBindingFn_New =
         module.Import(tProviderDict.Resolve()
                                    .GetConstructors()
                                    .First(
                                        c =>
                                        c.Parameters.Count == 1 &&
                                        c.Parameters[0].ParameterType.Name.StartsWith("IEqualityComparer")))
               .MakeHostInstanceGeneric(String, tFuncOfProviderBinding);
     DictionaryOfStringToProviderBindingFn_Add =
         module.Import(tProviderDict.Resolve().GetMethod("Add"))
               .MakeHostInstanceGeneric(String, tFuncOfProviderBinding);
     DictionaryOfStringToProviderBindingFn_TryGetValue =
         module.Import(tProviderDict.Resolve().GetMethod("TryGetValue"))
               .MakeHostInstanceGeneric(String, tFuncOfProviderBinding);
 }
예제 #3
0
        private void ImportInjectBindingDictionary(ModuleDefinition module, TypeReference tDict)
        {
            var tFuncOfBinding = FuncOfT.MakeGenericInstanceType(Binding);
            var tDictOfStringToBindingFn = tDict.MakeGenericInstanceType(
                String, tFuncOfBinding);

            DictionaryOfStringToBindingFn = tDictOfStringToBindingFn;
            DictionaryOfStringToBindingFn_New =
                module.Import(
                    tDictOfStringToBindingFn.Resolve()
                                            .Methods
                                            .First(
                                                c =>
                                                c.Name == ".ctor" &&
                                                c.Parameters.Count == 1 &&
                                                c.Parameters[0].ParameterType.Name.StartsWith("IEqualityComparer")))
                      .MakeHostInstanceGeneric(String, tFuncOfBinding);
            DictionaryOfStringToBindingFn_Add =
                module.Import(tDictOfStringToBindingFn.Resolve().GetMethod("Add"))
                      .MakeHostInstanceGeneric(String, tFuncOfBinding);
            DictionaryOfStringToBindingFn_TryGetValue =
                module.Import(tDictOfStringToBindingFn.Resolve().GetMethod("TryGetValue"))
                      .MakeHostInstanceGeneric(String, tFuncOfBinding);
        }
예제 #4
0
 /// <summary>
 /// Imports a method of a generic type.
 /// </summary>
 /// <returns>
 /// Returns an imported method with the given generic arguments applied to the declaring type.
 /// </returns>
 /// <param name="t">The type declaring the desired method.</param>
 /// <param name="predicate">A predicate identifying the desired method.  Must match one and only one method.</param>
 /// <param name="genericArguments">The generic arguments to be applied.</param>
 protected MethodReference ImportGeneric(TypeReference t, Func<MethodDefinition, bool> predicate, params TypeReference[] genericArguments)
 {
     var gt = t.MakeGenericInstanceType(genericArguments);
     return ModuleDefinition
         .Import(gt.Resolve().Methods.FirstOrDefault(predicate))
         .MakeHostInstanceGeneric(genericArguments);
 }
예제 #5
0
        private Cecil.TypeReference TypeReference(AnalysisNet.Types.IBasicType basicType)
        {
            if (typesCache.TryGetValue(basicType, out Cecil.TypeReference cecilTypeReference))
            {
                return(cecilTypeReference);
            }

            Cecil.TypeReference platformType = TypeReferenceToPlatformType(basicType);
            if (platformType != null)
            {
                typesCache[basicType] = platformType;
                return(platformType);
            }

            string nmspace = basicType.ContainingNamespace;
            string name    = basicType.MetadataName();

            Cecil.ModuleDefinition module = ResolveModule(basicType);
            Cecil.IMetadataScope   scope  = module ?? ResolveScope(basicType);
            if (module == null && scope == null)
            {
                throw new NotImplementedException();
            }

            cecilTypeReference = new Cecil.TypeReference(nmspace, name, module, scope);
            if (basicType.TypeKind == AnalysisNet.Types.TypeKind.ValueType)
            {
                cecilTypeReference.IsValueType = true;
            }

            if (basicType.ContainingType != null)
            {
                cecilTypeReference.DeclaringType = TypeReference(basicType.ContainingType);
                cecilTypeReference.Namespace     = string.Empty;
            }

            if (basicType.GenericParameterCount > 0)
            {
                Cecil.GenericInstanceType instantiated = null;
                // should we add constraints?
                cecilTypeReference.CreateGenericParameters(basicType.GenericParameterCount);
                MapGenericParameters(cecilTypeReference, basicType);
                // call it before instantiating it
                cecilTypeReference = ImportTypeReference(cecilTypeReference);

                if (basicType.GenericArguments.Count == 0)
                {
                    instantiated = cecilTypeReference.MakeGenericInstanceType(cecilTypeReference.GenericParameters.ToArray());
                }
                else
                {
                    Cecil.TypeReference[] arguments = basicType.GenericArguments.Select(ga => TypeReference(ga)).ToArray();
                    instantiated = cecilTypeReference.MakeGenericInstanceType(arguments);
                }

                cecilTypeReference = instantiated;
            }
            else
            {
                cecilTypeReference = ImportTypeReference(cecilTypeReference);
            }

            typesCache[basicType] = cecilTypeReference;
            return(cecilTypeReference);
        }
예제 #6
0
        private Tuple<TypeReference, MethodReference, MethodReference> GetFnMethods(
            TypeReference tFn,
            params TypeReference[] generics)
        {
            var t = Import(tFn.MakeGenericInstanceType(generics));

            var ctor = ImportGeneric(
                tFn,
                m => m.IsConstructor
                     && (m.Attributes & MethodAttributes.Public) == MethodAttributes.Public
                     && m.Parameters.Count == 2,
                generics);

            var invoke = ImportGeneric(
                tFn,
                m => m.Name == "Invoke",
                generics);

            return Tuple.Create(t, ctor, invoke);
        }