public XamlRuntimeContext(IXamlType definition, IXamlType constructedType,
                                  XamlLanguageEmitMappings <TBackendEmitter, TEmitResult> mappings,
                                  Action <XamlRuntimeContext <TBackendEmitter, TEmitResult>, TBackendEmitter> factory)
        {
            ContextType = definition.MakeGenericType(constructedType);

            IXamlField Get(string s) =>
            ContextType.Fields.FirstOrDefault(f => f.Name == s);

            IXamlMethod GetMethod(string s) =>
            ContextType.Methods.FirstOrDefault(f => f.Name == s);

            RootObjectField             = Get(XamlRuntimeContextDefintion.RootObjectFieldName);
            IntermediateRootObjectField = Get(XamlRuntimeContextDefintion.IntermediateRootObjectFieldName);
            ParentListField             = Get(XamlRuntimeContextDefintion.ParentListFieldName);
            PropertyTargetObject        = Get(XamlRuntimeContextDefintion.ProvideTargetObjectName);
            PropertyTargetProperty      = Get(XamlRuntimeContextDefintion.ProvideTargetPropertyName);
            PushParentMethod            = GetMethod(XamlRuntimeContextDefintion.PushParentMethodName);
            PopParentMethod             = GetMethod(XamlRuntimeContextDefintion.PopParentMethodName);
            Constructor = ContextType.Constructors.First();
            Factory     = il => factory(this, il);
            if (mappings.ContextFactoryCallback != null)
            {
                Factory = il =>
                {
                    factory(this, il);
                    mappings.ContextFactoryCallback(this, il);
                }
            }
            ;
        }
    }
Ejemplo n.º 2
0
 IXamlType LookupConverter(IXamlType type)
 {
     foreach (var p in _converters)
     {
         if (p.Key.Equals(type))
         {
             return(p.Value);
         }
     }
     if (type.GenericTypeDefinition?.Equals(_avaloniaList) == true)
     {
         return(_avaloniaListConverter.MakeGenericType(type.GenericArguments[0]));
     }
     return(null);
 }
Ejemplo n.º 3
0
 public static IXamlType MakeGenericType(this IXamlType type, params IXamlType[] typeArguments)
 => type.MakeGenericType(typeArguments);
        static XamlAstClrTypeReference ResolveTypeCore(AstTransformationContext context,
                                                       string xmlns, string name, bool isMarkupExtension, List <XamlAstXmlTypeReference> typeArguments, IXamlLineInfo lineInfo,
                                                       bool strict)
        {
            if (typeArguments == null)
            {
                typeArguments = new List <XamlAstXmlTypeReference>();
            }
            var targs = typeArguments
                        .Select(ta =>
                                ResolveType(context, ta.XmlNamespace, ta.Name, false, ta.GenericArguments, lineInfo, strict)
                                ?.Type)
                        .ToList();

            IXamlType Attempt(Func <string, IXamlType> cb, string xname)
            {
                var suffix = (typeArguments.Count != 0) ? ("`" + typeArguments.Count) : "";

                if (isMarkupExtension)
                {
                    return(cb(xname + "Extension" + suffix) ?? cb(xname + suffix));
                }
                else
                {
                    return(cb(xname + suffix) ?? cb(xname + "Extension" + suffix));
                }
            }

            IXamlType found = null;

            // Try to resolve from system
            if (xmlns == XamlNamespaces.Xaml2006)
            {
                found = context.Configuration.TypeSystem.FindType("System." + name);
            }


            if (found == null)
            {
                var resolvedNamespaces = NamespaceInfoHelper.TryResolve(context.Configuration, xmlns);
                if (resolvedNamespaces?.Count > 0)
                {
                    found = Attempt(formedName =>
                    {
                        foreach (var resolvedNs in resolvedNamespaces)
                        {
                            var rname        = resolvedNs.ClrNamespace + "." + formedName;
                            IXamlType subRes = null;
                            if (resolvedNs.Assembly != null)
                            {
                                subRes = resolvedNs.Assembly.FindType(rname);
                            }
                            else if (resolvedNs.AssemblyName != null)
                            {
                                subRes = context.Configuration.TypeSystem.FindType(rname, resolvedNs.AssemblyName);
                            }
                            else
                            {
                                foreach (var assembly in context.Configuration.TypeSystem.Assemblies)
                                {
                                    subRes = assembly.FindType(rname);
                                    if (subRes != null)
                                    {
                                        break;
                                    }
                                }
                            }
                            if (subRes != null)
                            {
                                return(subRes);
                            }
                        }

                        return(null);
                    }, name);
                }
            }

            if (typeArguments.Count != 0)
            {
                found = found?.MakeGenericType(targs);
            }
            if (found != null)
            {
                return(new XamlAstClrTypeReference(lineInfo, found,
                                                   isMarkupExtension || found.Name.EndsWith("Extension")));
            }
            if (strict)
            {
                throw new XamlParseException(
                          $"Unable to resolve type {name} from namespace {xmlns}", lineInfo);
            }
            return(null);
        }