From 6e51d690ce87930ca473aff2fb5dd2c6452565ed Mon Sep 17 00:00:00 2001 From: starfighter83 Date: Thu, 16 Dec 2010 11:15:56 -0100 Subject: [PATCH] XsltExtension attribute (now also searches in assemblies, not only app_code) --- umbraco/presentation/macro.cs | 102 +++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 40 deletions(-) diff --git a/umbraco/presentation/macro.cs b/umbraco/presentation/macro.cs index fc99a5376c..37f6f38820 100644 --- a/umbraco/presentation/macro.cs +++ b/umbraco/presentation/macro.cs @@ -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; }