Skip to content

Commit

Permalink
# improved ActivationManager class to load deployment assembly parts …
Browse files Browse the repository at this point in the history
…at startup according to the flag in the LoadAssemblyParts property
  • Loading branch information
Mariano Gabriel Converti committed Nov 13, 2011
1 parent 6ab95aa commit 60cd02b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
4 changes: 2 additions & 2 deletions SilverlightActivator.NuGet/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.4")]
[assembly: AssemblyFileVersion("1.0.4")]
[assembly: CLSCompliant(true)]
[assembly: NeutralResourcesLanguageAttribute("en-US")]
2 changes: 1 addition & 1 deletion SilverlightActivator.NuGet/SilverlightActivator.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>SilverlightActivator</id>
<version>1.0.3</version>
<version>1.0.4</version>
<title>Silverlight Activator for Windows Phone</title>
<authors>Mariano Converti, Damian Martinez Gelabert, and Nicolás Bello Camilletti</authors>
<owners>Mariano Converti, Damian Martinez Gelabert, and Nicolás Bello Camilletti</owners>
Expand Down
2 changes: 1 addition & 1 deletion SilverlightActivator.NuGet/content/App.xaml.transform
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
<silverlightactivator:ActivationManager x:Name="SilverlightActivator" />
<silverlightactivator:ActivationManager x:Name="SilverlightActivator" LoadAssemblyParts="False" />
</ResourceDictionary>
</Application.Resources>

Expand Down
56 changes: 50 additions & 6 deletions SilverlightActivator_WP/ActivationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
namespace SilverlightActivator
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;

/// <summary>
Expand All @@ -20,6 +23,8 @@ namespace SilverlightActivator
public class ActivationManager
{
private static bool initialized = false;
private static bool loadAssemblyParts = false;
private static bool assemblyPartsLoaded = false;

/// <summary>
/// Initializes a new instance of the SilverlightActivator.ActivationManager class, containing the activation mechanisms.
Expand All @@ -33,6 +38,17 @@ public ActivationManager()
}
}

/// <summary>
/// Gets or Sets a flag indicating if all the assembly parts in the
/// Application Manifest should be loaded at startup to look for
/// activation attributes.
/// </summary>
public bool LoadAssemblyParts
{
get { return loadAssemblyParts; }
set { loadAssemblyParts = value; }
}

/// <summary>
/// Runs all the available ApplicationStartup methods.
/// </summary>
Expand All @@ -58,18 +74,46 @@ private static void Init()
private static void RunActivationMethods<T>() where T : BaseActivationMethodAttribute
{
var deploymentParts = Deployment.Current.Parts.Cast<AssemblyPart>();

if (loadAssemblyParts && !assemblyPartsLoaded)
{
Load(deploymentParts);
assemblyPartsLoaded = true;
}

// Filter loaded assemblies to only get the deployment assembly parts.
var activationAssemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => deploymentParts.Any(p => p.Source.Equals(a.ManifestModule.Name, StringComparison.OrdinalIgnoreCase))
&& (a != typeof(ActivationManager).Assembly));
&& (a != typeof(ActivationManager).Assembly))
.ToList();
var activationAttributes = new List<T>();

// Iterate throughout all the loaded deployment assembly parts to look for activation attributes.
activationAssemblies.ForEach(assembly => activationAttributes.AddRange(assembly.GetActivationAttributes<T>()));

// Execute activation methods according to the order specified.
foreach (var attribute in activationAttributes.OrderBy(at => at.Order))
{
attribute.InvokeMethod();
}
}

foreach (var assembly in activationAssemblies)
private static void Load(IEnumerable<AssemblyPart> deploymentParts)
{
foreach (var assemblyPart in deploymentParts)
{
// The activation methods are executed according to the specified order
var activationAttributes = assembly.GetActivationAttributes<T>().OrderBy(at => at.Order);
foreach (var attribute in activationAttributes)
var assemblyString = assemblyPart.Source
.ToUpperInvariant()
.Replace(".DLL", string.Empty);

try
{
attribute.InvokeMethod();
if (!string.IsNullOrWhiteSpace(assemblyString))
Assembly.Load(assemblyString);
}
catch (FileNotFoundException) { }
catch (FileLoadException) { }
catch (BadImageFormatException) { }
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions SilverlightActivator_WP/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.4")]
[assembly: AssemblyFileVersion("1.0.4")]
[assembly: CLSCompliant(true)]
[assembly: NeutralResourcesLanguageAttribute("en-US")]
3 changes: 2 additions & 1 deletion SilverlightActivator_WP/Properties/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
// You do not need to add suppressions to this file manually.

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1053:StaticHolderTypesShouldNotHaveConstructors", Scope = "type", Target = "SilverlightActivator.ActivationManager", Justification = "This class cannot be static and must contain one default public constructor since it is added as an application resource in the App.xaml file.")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "SilverlightActivator", Justification = "There is only one namespace in the assembly")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "SilverlightActivator", Justification = "There is only one namespace in the assembly.")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Scope = "member", Target = "SilverlightActivator.ActivationManager.#LoadAssemblyParts", Justification = "This property is intended to be set when added as an application resource in the App.xaml file.")]

0 comments on commit 60cd02b

Please sign in to comment.