Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In my Emerald Data Foundation (EDF) tool I solved this by creating a more powerful MarkupExtension than x:Type that could specify generic types too. That way I could write:</p> <pre><code> &lt;DataTemplate TargetType="{edf:Type generic:ICollection{local:Entity}}" /&gt; </code></pre> <p>Here's what I used:</p> <pre><code> [MarkupExtensionReturnType(typeof(Type))] public class TypeExtension : MarkupExtension { public TypeExtension() { } public TypeExtension(string typeName) { TypeName = typeName; } public TypeExtension(Type type) { Type = type; } public string TypeName { get; set; } public Type Type { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { if(Type==null) { IXamlTypeResolver typeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver; if(typeResolver==null) throw new InvalidOperationException("EDF Type markup extension used without XAML context"); if(TypeName==null) throw new InvalidOperationException("EDF Type markup extension used without Type or TypeName"); Type = ResolveGenericTypeName(TypeName, (name) =&gt; { Type result = typeResolver.Resolve(name); if(result==null) throw new Exception("EDF Type markup extension could not resolve type " + name); return result; }); } return Type; } public static Type ResolveGenericTypeName(string name, Func&lt;string, Type&gt; resolveSimpleName) { if(name.Contains('{')) name = name.Replace('{', '&lt;').Replace('}', '&gt;'); // Note: For convenience working with XAML, we allow {} instead of &lt;&gt; for generic type parameters if(name.Contains('&lt;')) { var match = _genericTypeRegex.Match(name); if(match.Success) { Type[] typeArgs = ( from arg in match.Groups["typeArgs"].Value.SplitOutsideParenthesis(',') select ResolveGenericTypeName(arg, resolveSimpleName) ).ToArray(); string genericTypeName = match.Groups["genericTypeName"].Value + "`" + typeArgs.Length; Type genericType = resolveSimpleName(genericTypeName); if(genericType!=null &amp;&amp; !typeArgs.Contains(null)) return genericType.MakeGenericType(typeArgs); } } return resolveSimpleName(name); } static Regex _genericTypeRegex = new Regex(@"^(?&lt;genericTypeName&gt;\w+)&lt;(?&lt;typeArgs&gt;\w+(,\w+)*)&gt;$"); } </code></pre> <p>The generic type name parsing code is in a separate method because it is also used by some other code in EDF. You could combine it all into one method.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload