2 using System.Collections.Generic;
5 using System.Reflection;
7 using AppLimit.NetSparkle.Interfaces;
9 namespace AppLimit.NetSparkle
11 public class NetSparkleAssemblyReflectionAccessor : INetSparkleAssemblyAccessor
13 private Assembly _assembly;
14 private List<Attribute> _assemblyAttributes = new List<Attribute>();
16 public NetSparkleAssemblyReflectionAccessor(String assemblyName)
18 if (assemblyName == null)
19 _assembly = Assembly.GetEntryAssembly();
22 String absolutePath = Path.GetFullPath(assemblyName);
23 if (!File.Exists(absolutePath))
24 throw new FileNotFoundException();
26 _assembly = Assembly.ReflectionOnlyLoadFrom(absolutePath);
28 if (_assembly == null)
29 throw new Exception("Unable to load assembly " + absolutePath);
32 // read the attributes
33 foreach (CustomAttributeData data in _assembly.GetCustomAttributesData())
34 _assemblyAttributes.Add(CreateAttribute(data));
36 if (_assemblyAttributes == null || _assemblyAttributes.Count == 0)
37 throw new Exception("Unable to load assembly attributes from " + _assembly.FullName);
41 /// This methods creates an attribute instance from the attribute data
44 /// <param name="data"></param>
45 /// <returns></returns>
46 private Attribute CreateAttribute(CustomAttributeData data)
48 var arguments = from arg in data.ConstructorArguments
51 var attribute = data.Constructor.Invoke(arguments.ToArray())
54 foreach (var namedArgument in data.NamedArguments)
56 var propertyInfo = namedArgument.MemberInfo as PropertyInfo;
57 if (propertyInfo != null)
59 propertyInfo.SetValue(attribute, namedArgument.TypedValue.Value, null);
63 var fieldInfo = namedArgument.MemberInfo as FieldInfo;
64 if (fieldInfo != null)
66 fieldInfo.SetValue(attribute, namedArgument.TypedValue.Value);
74 private Attribute FindAttribute(Type AttributeType)
76 foreach (Attribute attr in _assemblyAttributes)
78 if (attr.GetType().Equals(AttributeType))
82 throw new Exception("Attribute of type " + AttributeType.ToString() + " does not exists in the assembly " + _assembly.FullName);
85 #region Assembly Attribute Accessors
87 public string AssemblyTitle
91 AssemblyTitleAttribute a = FindAttribute(typeof(AssemblyTitleAttribute)) as AssemblyTitleAttribute;
96 public string AssemblyVersion
100 return _assembly.GetName().Version.ToString();
104 public string AssemblyDescription
108 AssemblyDescriptionAttribute a = FindAttribute(typeof(AssemblyDescriptionAttribute)) as AssemblyDescriptionAttribute;
109 return a.Description;
113 public string AssemblyProduct
117 AssemblyProductAttribute a = FindAttribute(typeof(AssemblyProductAttribute)) as AssemblyProductAttribute;
122 public string AssemblyCopyright
126 AssemblyCopyrightAttribute a = FindAttribute(typeof(AssemblyCopyrightAttribute)) as AssemblyCopyrightAttribute;
131 public string AssemblyCompany
135 AssemblyCompanyAttribute a = FindAttribute(typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;