Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.7 kB)

1
´╗┐using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Reflection;
6
using System.IO;
7
using AppLimit.NetSparkle.Interfaces;
8

    
9
namespace AppLimit.NetSparkle
10
{
11
    public class NetSparkleAssemblyReflectionAccessor : INetSparkleAssemblyAccessor
12
    {
13
        private Assembly _assembly;
14
        private List<Attribute> _assemblyAttributes = new List<Attribute>();
15

    
16
        public NetSparkleAssemblyReflectionAccessor(String assemblyName)
17
        {
18
            if (assemblyName == null)
19
                _assembly = Assembly.GetEntryAssembly();
20
            else
21
            {
22
                String absolutePath = Path.GetFullPath(assemblyName);
23
                if (!File.Exists(absolutePath))
24
                    throw new FileNotFoundException();
25

    
26
                _assembly = Assembly.ReflectionOnlyLoadFrom(absolutePath);
27

    
28
                if (_assembly == null)
29
                    throw new Exception("Unable to load assembly " + absolutePath);                
30
            }
31

    
32
            // read the attributes            
33
            foreach (CustomAttributeData data in _assembly.GetCustomAttributesData())
34
                _assemblyAttributes.Add(CreateAttribute(data));
35

    
36
            if (_assemblyAttributes == null || _assemblyAttributes.Count == 0)
37
                throw new Exception("Unable to load assembly attributes from " + _assembly.FullName);                                    
38
        }
39

    
40
        /// <summary>
41
        /// This methods creates an attribute instance from the attribute data 
42
        /// information
43
        /// </summary>
44
        /// <param name="data"></param>
45
        /// <returns></returns>
46
        private Attribute CreateAttribute(CustomAttributeData data)
47
        {
48
            var arguments = from arg in data.ConstructorArguments
49
                            select arg.Value;
50

    
51
            var attribute = data.Constructor.Invoke(arguments.ToArray())
52
              as Attribute;
53

    
54
            foreach (var namedArgument in data.NamedArguments)
55
            {
56
                var propertyInfo = namedArgument.MemberInfo as PropertyInfo;
57
                if (propertyInfo != null)
58
                {
59
                    propertyInfo.SetValue(attribute, namedArgument.TypedValue.Value, null);
60
                }
61
                else
62
                {
63
                    var fieldInfo = namedArgument.MemberInfo as FieldInfo;
64
                    if (fieldInfo != null)
65
                    {
66
                        fieldInfo.SetValue(attribute, namedArgument.TypedValue.Value);
67
                    }
68
                }
69
            }
70

    
71
            return attribute;
72
        }
73

    
74
        private Attribute FindAttribute(Type AttributeType)
75
        {            
76
            foreach (Attribute attr in _assemblyAttributes)
77
            {
78
                if (attr.GetType().Equals(AttributeType))
79
                    return attr;                                
80
            }
81

    
82
            throw new Exception("Attribute of type " + AttributeType.ToString() + " does not exists in the assembly " + _assembly.FullName);
83
        }
84

    
85
        #region Assembly Attribute Accessors
86

    
87
        public string AssemblyTitle
88
        {
89
            get
90
            {
91
                AssemblyTitleAttribute a = FindAttribute(typeof(AssemblyTitleAttribute)) as AssemblyTitleAttribute;
92
                return a.Title;                
93
            }
94
        }
95

    
96
        public string AssemblyVersion
97
        {
98
            get
99
            {
100
                return _assembly.GetName().Version.ToString();
101
            }
102
        }        
103

    
104
        public string AssemblyDescription
105
        {
106
            get
107
            {
108
                AssemblyDescriptionAttribute a = FindAttribute(typeof(AssemblyDescriptionAttribute)) as AssemblyDescriptionAttribute;
109
                return a.Description;                                
110
            }
111
        }
112

    
113
        public string AssemblyProduct
114
        {
115
            get
116
            {
117
                AssemblyProductAttribute a = FindAttribute(typeof(AssemblyProductAttribute)) as AssemblyProductAttribute;
118
                return a.Product;                                
119
            }
120
        }
121

    
122
        public string AssemblyCopyright
123
        {
124
            get
125
            {
126
                AssemblyCopyrightAttribute a = FindAttribute(typeof(AssemblyCopyrightAttribute)) as AssemblyCopyrightAttribute;
127
                return a.Copyright;                                                
128
            }
129
        }
130

    
131
        public string AssemblyCompany
132
        {
133
            get
134
            {
135
                AssemblyCompanyAttribute a = FindAttribute(typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute;
136
                return a.Company;                  
137
            }
138
        }
139
        #endregion
140
    }
141
}