XsltExtension attribute (now also searches in assemblies, not only app_code)

This commit is contained in:
starfighter83
2010-12-16 11:15:56 -01:00
parent 6a69202f5e
commit 6e51d690ce

View File

@@ -824,58 +824,80 @@ namespace umbraco
}
}
Assembly appCodeAssembly;
//also get types marked with XsltExtension attribute
try
{
if (Directory.Exists(GlobalSettings.FullpathToRoot + Path.DirectorySeparatorChar + "App_Code"))
foreach (Type xsltType in BusinessLogic.Utils.TypeFinder.FindClassesMarkedWithAttribute(typeof(XsltExtensionAttribute)))
{
if (Directory.GetFiles(GlobalSettings.FullpathToRoot + Path.DirectorySeparatorChar + "App_Code",
"*.*",
SearchOption.AllDirectories).Length > 0)
object[] tpAttributes = xsltType.GetCustomAttributes(typeof(XsltExtensionAttribute), true);
foreach (XsltExtensionAttribute tpAttribute in tpAttributes)
{
appCodeAssembly = Assembly.Load("__code");
Type[] appCodeType = appCodeAssembly.GetExportedTypes();
if (appCodeType.Length == 0)
if (tpAttribute.Namespace != String.Empty)
{
Log.Add(LogTypes.System, Node.GetCurrent().Id,
String.Format(
"Could not load types in App_Code ({0}) for XSLT extensions. Ensure you have used the public keyword to ensure class and method exposure.",
appCodeAssembly.FullName));
extensions.Add(tpAttribute.Namespace, Activator.CreateInstance(xsltType));
}
else
{
// create an instance and add it to the extensions list
foreach (Type tp in appCodeType)
{
object[] tpAttributes = tp.GetCustomAttributes(typeof(XsltExtensionAttribute), true);
foreach (XsltExtensionAttribute tpAttribute in tpAttributes)
{
if (tpAttribute.Namespace != String.Empty)
{
extensions.Add(tpAttribute.Namespace, Activator.CreateInstance(tp));
}
else
{
extensions.Add(tp.FullName, Activator.CreateInstance(tp));
}
}
}
extensions.Add(xsltType.FullName, Activator.CreateInstance(xsltType));
}
}
}
else
{
Directory.CreateDirectory(GlobalSettings.FullpathToRoot + Path.DirectorySeparatorChar + "App_Code");
}
}
catch (FileNotFoundException)
{
//Do nothing - just means there's nothing to load.
}
catch (Exception ex)
{
throw new Exception("Could not load App_Code classes for XSLT extensions.", ex);
}
catch { }
//Assembly appCodeAssembly;
//try
//{
// if (Directory.Exists(GlobalSettings.FullpathToRoot + Path.DirectorySeparatorChar + "App_Code"))
// {
// if (Directory.GetFiles(GlobalSettings.FullpathToRoot + Path.DirectorySeparatorChar + "App_Code",
// "*.*",
// SearchOption.AllDirectories).Length > 0)
// {
// appCodeAssembly = Assembly.Load("__code");
// Type[] appCodeType = appCodeAssembly.GetExportedTypes();
// if (appCodeType.Length == 0)
// {
// Log.Add(LogTypes.System, Node.GetCurrent().Id,
// String.Format(
// "Could not load types in App_Code ({0}) for XSLT extensions. Ensure you have used the public keyword to ensure class and method exposure.",
// appCodeAssembly.FullName));
// }
// else
// {
// // create an instance and add it to the extensions list
// foreach (Type tp in appCodeType)
// {
// object[] tpAttributes = tp.GetCustomAttributes(typeof(XsltExtensionAttribute), true);
// foreach (XsltExtensionAttribute tpAttribute in tpAttributes)
// {
// if (tpAttribute.Namespace != String.Empty)
// {
// extensions.Add(tpAttribute.Namespace, Activator.CreateInstance(tp));
// }
// else
// {
// extensions.Add(tp.FullName, Activator.CreateInstance(tp));
// }
// }
// }
// }
// }
// }
// else
// {
// Directory.CreateDirectory(GlobalSettings.FullpathToRoot + Path.DirectorySeparatorChar + "App_Code");
// }
//}
//catch (FileNotFoundException)
//{
// //Do nothing - just means there's nothing to load.
//}
//catch (Exception ex)
//{
// throw new Exception("Could not load App_Code classes for XSLT extensions.", ex);
//}
return extensions;
}