Statistics
| Branch: | Revision:

root / trunk / NetSparkle / NetSparkleAssemblyReflectionAccessor.cs @ 049333d2

History | View | Annotate | Download (4.7 kB)

1 46426dbd Panagiotis Kanavos
´╗┐using System;
2 46426dbd Panagiotis Kanavos
using System.Collections.Generic;
3 46426dbd Panagiotis Kanavos
using System.Linq;
4 46426dbd Panagiotis Kanavos
using System.Text;
5 46426dbd Panagiotis Kanavos
using System.Reflection;
6 46426dbd Panagiotis Kanavos
using System.IO;
7 46426dbd Panagiotis Kanavos
using AppLimit.NetSparkle.Interfaces;
8 46426dbd Panagiotis Kanavos
9 46426dbd Panagiotis Kanavos
namespace AppLimit.NetSparkle
10 46426dbd Panagiotis Kanavos
{
11 46426dbd Panagiotis Kanavos
    public class NetSparkleAssemblyReflectionAccessor : INetSparkleAssemblyAccessor
12 46426dbd Panagiotis Kanavos
    {
13 46426dbd Panagiotis Kanavos
        private Assembly _assembly;
14 46426dbd Panagiotis Kanavos
        private List<Attribute> _assemblyAttributes = new List<Attribute>();
15 46426dbd Panagiotis Kanavos
16 46426dbd Panagiotis Kanavos
        public NetSparkleAssemblyReflectionAccessor(String assemblyName)
17 46426dbd Panagiotis Kanavos
        {
18 46426dbd Panagiotis Kanavos
            if (assemblyName == null)
19 46426dbd Panagiotis Kanavos
                _assembly = Assembly.GetEntryAssembly();
20 46426dbd Panagiotis Kanavos
            else
21 46426dbd Panagiotis Kanavos
            {
22 46426dbd Panagiotis Kanavos
                String absolutePath = Path.GetFullPath(assemblyName);
23 46426dbd Panagiotis Kanavos
                if (!File.Exists(absolutePath))
24 46426dbd Panagiotis Kanavos
                    throw new FileNotFoundException();
25 46426dbd Panagiotis Kanavos
26 46426dbd Panagiotis Kanavos
                _assembly = Assembly.ReflectionOnlyLoadFrom(absolutePath);
27 46426dbd Panagiotis Kanavos
28 46426dbd Panagiotis Kanavos
                if (_assembly == null)
29 46426dbd Panagiotis Kanavos
                    throw new Exception("Unable to load assembly " + absolutePath);                
30 46426dbd Panagiotis Kanavos
            }
31 46426dbd Panagiotis Kanavos
32 46426dbd Panagiotis Kanavos
            // read the attributes            
33 46426dbd Panagiotis Kanavos
            foreach (CustomAttributeData data in _assembly.GetCustomAttributesData())
34 46426dbd Panagiotis Kanavos
                _assemblyAttributes.Add(CreateAttribute(data));
35 46426dbd Panagiotis Kanavos
36 46426dbd Panagiotis Kanavos
            if (_assemblyAttributes == null || _assemblyAttributes.Count == 0)
37 46426dbd Panagiotis Kanavos
                throw new Exception("Unable to load assembly attributes from " + _assembly.FullName);                                    
38 46426dbd Panagiotis Kanavos
        }
39 46426dbd Panagiotis Kanavos
40 46426dbd Panagiotis Kanavos
        /// <summary>
41 46426dbd Panagiotis Kanavos
        /// This methods creates an attribute instance from the attribute data 
42 46426dbd Panagiotis Kanavos
        /// information
43 46426dbd Panagiotis Kanavos
        /// </summary>
44 46426dbd Panagiotis Kanavos
        /// <param name="data"></param>
45 46426dbd Panagiotis Kanavos
        /// <returns></returns>
46 46426dbd Panagiotis Kanavos
        private Attribute CreateAttribute(CustomAttributeData data)
47 46426dbd Panagiotis Kanavos
        {
48 46426dbd Panagiotis Kanavos
            var arguments = from arg in data.ConstructorArguments
49 46426dbd Panagiotis Kanavos
                            select arg.Value;
50 46426dbd Panagiotis Kanavos
51 46426dbd Panagiotis Kanavos
            var attribute = data.Constructor.Invoke(arguments.ToArray())
52 46426dbd Panagiotis Kanavos
              as Attribute;
53 46426dbd Panagiotis Kanavos
54 46426dbd Panagiotis Kanavos
            foreach (var namedArgument in data.NamedArguments)
55 46426dbd Panagiotis Kanavos
            {
56 46426dbd Panagiotis Kanavos
                var propertyInfo = namedArgument.MemberInfo as PropertyInfo;
57 46426dbd Panagiotis Kanavos
                if (propertyInfo != null)
58 46426dbd Panagiotis Kanavos
                {
59 46426dbd Panagiotis Kanavos
                    propertyInfo.SetValue(attribute, namedArgument.TypedValue.Value, null);
60 46426dbd Panagiotis Kanavos
                }
61 46426dbd Panagiotis Kanavos
                else
62 46426dbd Panagiotis Kanavos
                {
63 46426dbd Panagiotis Kanavos
                    var fieldInfo = namedArgument.MemberInfo as FieldInfo;
64 46426dbd Panagiotis Kanavos
                    if (fieldInfo != null)
65 46426dbd Panagiotis Kanavos
                    {
66 46426dbd Panagiotis Kanavos
                        fieldInfo.SetValue(attribute, namedArgument.TypedValue.Value);
67 46426dbd Panagiotis Kanavos
                    }
68 46426dbd Panagiotis Kanavos
                }
69 46426dbd Panagiotis Kanavos
            }
70 46426dbd Panagiotis Kanavos
71 46426dbd Panagiotis Kanavos
            return attribute;
72 46426dbd Panagiotis Kanavos
        }
73 46426dbd Panagiotis Kanavos
74 46426dbd Panagiotis Kanavos
        private Attribute FindAttribute(Type AttributeType)
75 46426dbd Panagiotis Kanavos
        {            
76 46426dbd Panagiotis Kanavos
            foreach (Attribute attr in _assemblyAttributes)
77 46426dbd Panagiotis Kanavos
            {
78 46426dbd Panagiotis Kanavos
                if (attr.GetType().Equals(AttributeType))
79 46426dbd Panagiotis Kanavos
                    return attr;                                
80 46426dbd Panagiotis Kanavos
            }
81 46426dbd Panagiotis Kanavos
82 46426dbd Panagiotis Kanavos
            throw new Exception("Attribute of type " + AttributeType.ToString() + " does not exists in the assembly " + _assembly.FullName);
83 46426dbd Panagiotis Kanavos
        }
84 46426dbd Panagiotis Kanavos
85 46426dbd Panagiotis Kanavos
        #region Assembly Attribute Accessors
86 46426dbd Panagiotis Kanavos
87 46426dbd Panagiotis Kanavos
        public string AssemblyTitle
88 46426dbd Panagiotis Kanavos
        {
89 46426dbd Panagiotis Kanavos
            get
90 46426dbd Panagiotis Kanavos
            {
91 46426dbd Panagiotis Kanavos
                AssemblyTitleAttribute a = FindAttribute(typeof(AssemblyTitleAttribute)) as AssemblyTitleAttribute;
92 46426dbd Panagiotis Kanavos
                return a.Title;                
93 46426dbd Panagiotis Kanavos
            }
94 46426dbd Panagiotis Kanavos
        }
95 46426dbd Panagiotis Kanavos
96 46426dbd Panagiotis Kanavos
        public string AssemblyVersion
97 46426dbd Panagiotis Kanavos
        {
98 46426dbd Panagiotis Kanavos
            get
99 46426dbd Panagiotis Kanavos
            {
100 46426dbd Panagiotis Kanavos
                return _assembly.GetName().Version.ToString();
101 46426dbd Panagiotis Kanavos
            }
102 46426dbd Panagiotis Kanavos
        }        
103 46426dbd Panagiotis Kanavos
104 46426dbd Panagiotis Kanavos
        public string AssemblyDescription
105 46426dbd Panagiotis Kanavos
        {
106 46426dbd Panagiotis Kanavos
            get
107 46426dbd Panagiotis Kanavos
            {
108 46426dbd Panagiotis Kanavos
                AssemblyDescriptionAttribute a = FindAttribute(typeof(AssemblyDescriptionAttribute)) as AssemblyDescriptionAttribute;
109 46426dbd Panagiotis Kanavos
                return a.Description;                                
110 46426dbd Panagiotis Kanavos
            }
111 46426dbd Panagiotis Kanavos
        }
112 46426dbd Panagiotis Kanavos
113 46426dbd Panagiotis Kanavos
        public string AssemblyProduct
114 46426dbd Panagiotis Kanavos
        {
115 46426dbd Panagiotis Kanavos
            get
116 46426dbd Panagiotis Kanavos
            {
117 46426dbd Panagiotis Kanavos
                AssemblyProductAttribute a = FindAttribute(typeof(AssemblyProductAttribute)) as AssemblyProductAttribute;
118 46426dbd Panagiotis Kanavos
                return a.Product;                                
119 46426dbd Panagiotis Kanavos
            }
120 46426dbd Panagiotis Kanavos
        }
121 46426dbd Panagiotis Kanavos
122 46426dbd Panagiotis Kanavos
        public string AssemblyCopyright
123 46426dbd Panagiotis Kanavos
        {
124 46426dbd Panagiotis Kanavos
            get
125 46426dbd Panagiotis Kanavos
            {
126 46426dbd Panagiotis Kanavos
                AssemblyCopyrightAttribute a = FindAttribute(typeof(AssemblyCopyrightAttribute)) as AssemblyCopyrightAttribute;
127 46426dbd Panagiotis Kanavos
                return a.Copyright;                                                
128 46426dbd Panagiotis Kanavos
            }
129 46426dbd Panagiotis Kanavos
        }
130 46426dbd Panagiotis Kanavos
131 46426dbd Panagiotis Kanavos
        public string AssemblyCompany
132 46426dbd Panagiotis Kanavos
        {
133 46426dbd Panagiotis Kanavos
            get
134 46426dbd Panagiotis Kanavos
            {
135 46426dbd Panagiotis Kanavos
                AssemblyCompanyAttribute a = FindAttribute(typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
136 46426dbd Panagiotis Kanavos
                return a.Company;                  
137 46426dbd Panagiotis Kanavos
            }
138 46426dbd Panagiotis Kanavos
        }
139 46426dbd Panagiotis Kanavos
        #endregion
140 46426dbd Panagiotis Kanavos
    }
141 46426dbd Panagiotis Kanavos
}