Ejemplo n.º 1
0
        public IEnumerable <Instruction> ConvertFromString(string value, ILContext context, BaseNode node)
        {
            var module = context.Module;

            EmbeddedResource matchedResource = null;

            foreach (var resource in module.Resources.OfType <EmbeddedResource>())
            {
                if (resource.Name.StartsWith(context.EmbeddedResourceNameSpace) && resource.Name.EndsWith(value))
                {
                    matchedResource = resource;
                    break;
                }
            }

            if (null == matchedResource)
            {
                foreach (var resource in module.Resources.OfType <EmbeddedResource>())
                {
                    if (resource.Name.EndsWith(value))
                    {
                        matchedResource = resource;
                        break;
                    }
                }
            }

            if (null != matchedResource)
            {
                string classname;
                if (matchedResource.IsResourceDictionaryXaml(module, out classname))
                {
                    int lastIndex     = classname.LastIndexOf('.');
                    var realClassName = classname.Substring(lastIndex + 1);
                    var typeref       = XmlTypeExtensions.GetTypeReference(realClassName, module, node, XmlTypeExtensions.ModeOfGetType.Both);

                    var typeName             = matchedResource.Name.Replace('.', '_');
                    var typeDefOfGetResource = module.Types.FirstOrDefault(type => type.FullName == "GetResource." + typeName);
                    if (null != typeDefOfGetResource)
                    {
                        module.Types.Remove(typeDefOfGetResource);
                        typeDefOfGetResource = null;
                    }

                    if (null == typeDefOfGetResource)
                    {
                        typeDefOfGetResource          = new TypeDefinition("GetResource", typeName, TypeAttributes.NotPublic);
                        typeDefOfGetResource.BaseType = typeref;
                        module.Types.Add(typeDefOfGetResource);

                        typeDefOfGetResource.AddDefaultConstructor(typeref);
                    }

                    var methodName          = "GetResource";
                    var methodOfGetResource = typeDefOfGetResource.Methods.FirstOrDefault(m => m.Name == methodName);

                    if (null == methodOfGetResource)
                    {
                        methodOfGetResource = new MethodDefinition(methodName, MethodAttributes.Public, typeref);
                        typeDefOfGetResource.Methods.Add(methodOfGetResource);
                    }

                    var constructor = typeDefOfGetResource.Methods.FirstOrDefault(m => m.IsConstructor);

                    if (null != constructor)
                    {
                        constructor.Body.Instructions.Insert(constructor.Body.Instructions.Count - 1, Instruction.Create(OpCodes.Ldarg_0));
                        constructor.Body.Instructions.Insert(constructor.Body.Instructions.Count - 1, Instruction.Create(OpCodes.Call, methodOfGetResource));
                        constructor.Body.Instructions.Insert(constructor.Body.Instructions.Count - 1, Instruction.Create(OpCodes.Pop));
                    }

                    var rootnode = XamlTask.ParseXaml(matchedResource.GetResourceStream(), typeref);

                    Exception exception;
                    TryCoreCompile(methodOfGetResource, rootnode, context.EmbeddedResourceNameSpace, out exception);

                    yield return(Create(Newobj, constructor));
                }
            }
        }