1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.Xaml.Hosting
{
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime;
using System.Text;
using System.Web;
using System.Web.Compilation;
using System.Web.Hosting;
using System.Xaml;
using System.Xaml.Hosting.Configuration;
using System.Xml;
[BuildProviderAppliesTo(BuildProviderAppliesTo.Web)]
public class XamlBuildProvider : BuildProvider
{
IXamlBuildProviderExtension xamlBuildProviderExtension;
static volatile IXamlBuildProviderExtensionFactory xamlBuildProviderExtensionFactory;
internal new string VirtualPath
{
get { return base.VirtualPath; }
}
public override void GenerateCode(AssemblyBuilder assemblyBuilder)
{
XamlType rootXamlType = GetRootXamlType();
this.GetXamlBuildProviderExtension(rootXamlType);
if (this.xamlBuildProviderExtension != null)
{
using (Stream xamlStream = base.OpenStream())
{
this.xamlBuildProviderExtension.GenerateCode(assemblyBuilder, xamlStream, this);
}
}
}
[Fx.Tag.Throws(typeof(TypeLoadException), "The type resolution of the root element failed.")]
public override Type GetGeneratedType(CompilerResults results)
{
if (this.xamlBuildProviderExtension != null)
{
Type result = this.xamlBuildProviderExtension.GetGeneratedType(results);
if (result != null)
{
return result;
}
}
try
{
XamlType rootXamlType = GetRootXamlType();
if (rootXamlType.IsUnknown)
{
StringBuilder typeName = new StringBuilder();
AppendTypeName(rootXamlType, typeName);
throw FxTrace.Exception.AsError(new TypeLoadException(SR.CouldNotResolveType(typeName)));
}
return rootXamlType.UnderlyingType;
}
catch (XamlParseException ex)
{
throw FxTrace.Exception.AsError(new HttpCompileException(ex.Message, ex));
}
}
public override BuildProviderResultFlags GetResultFlags(CompilerResults results)
{
return BuildProviderResultFlags.ShutdownAppDomainOnChange;
}
private void AppendTypeName(XamlType xamlType, StringBuilder sb)
{
if (!string.IsNullOrEmpty(xamlType.PreferredXamlNamespace))
{
sb.Append("{");
sb.Append(xamlType.PreferredXamlNamespace);
sb.Append("}");
}
sb.Append(xamlType.Name);
if (xamlType.IsGeneric)
{
sb.Append("(");
for (int i = 0; i < xamlType.TypeArguments.Count; i++)
{
AppendTypeName(xamlType.TypeArguments[i], sb);
if (i < xamlType.TypeArguments.Count - 1)
{
sb.Append(", ");
}
}
sb.Append(")");
}
}
XamlType GetRootXamlType()
{
try
{
using (Stream xamlStream = base.OpenStream())
{
XmlReader xmlReader = XmlReader.Create(xamlStream);
XamlXmlReader xamlReader = new XamlXmlReader(xmlReader);
// Read to the root object
while (xamlReader.Read())
{
if (xamlReader.NodeType == XamlNodeType.StartObject)
{
return xamlReader.Type;
}
}
throw FxTrace.Exception.AsError(new HttpCompileException(SR.UnexpectedEof));
}
}
catch (XamlParseException ex)
{
throw FxTrace.Exception.AsError(new HttpCompileException(ex.Message, ex));
}
}
void GetXamlBuildProviderExtension(XamlType rootXamlType)
{
if (rootXamlType.UnderlyingType == null)
{
return;
}
this.GetXamlBuildProviderExtensionFactory(rootXamlType);
if (xamlBuildProviderExtensionFactory != null)
{
this.xamlBuildProviderExtension = xamlBuildProviderExtensionFactory.GetXamlBuildProviderExtension();
}
}
void GetXamlBuildProviderExtensionFactory(XamlType rootXamlType)
{
if (xamlBuildProviderExtensionFactory != null)
{
return;
}
// Get the HttpHandler type
Type httpHandlerType;
XamlHostingConfiguration.TryGetHttpHandlerType(this.VirtualPath, rootXamlType.UnderlyingType, out httpHandlerType);
if (httpHandlerType != null && typeof(IXamlBuildProviderExtensionFactory).IsAssignableFrom(httpHandlerType))
{
xamlBuildProviderExtensionFactory = (IXamlBuildProviderExtensionFactory)Activator.CreateInstance(httpHandlerType,
BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
null, null, null);
}
}
}
}
|