~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/ReflectionExtensions.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Diagnostics;
 
3
using System.Linq;
 
4
using System.Reflection;
 
5
using ServiceStack.Common.Utils;
 
6
 
 
7
namespace ServiceStack.Common
 
8
{
 
9
    public static class ReflectionExtensions
 
10
    {
 
11
        public static To PopulateWith<To, From>(this To to, From from)
 
12
        {
 
13
            return ReflectionUtils.PopulateObject(to, from);
 
14
        }
 
15
 
 
16
        public static To PopulateWithNonDefaultValues<To, From>(this To to, From from)
 
17
        {
 
18
            return ReflectionUtils.PopulateWithNonDefaultValues(to, from);
 
19
        }
 
20
 
 
21
        public static To PopulateFromPropertiesWithAttribute<To, From, TAttr>(this To to, From from)
 
22
        {
 
23
            return ReflectionUtils.PopulateFromPropertiesWithAttribute(to, from, typeof(TAttr));
 
24
        }
 
25
 
 
26
        public static T TranslateTo<T>(this object from)
 
27
            where T : new()
 
28
        {
 
29
            var to = new T();
 
30
            return to.PopulateWith(from);
 
31
        }
 
32
 
 
33
        public static TAttribute FirstAttribute<TAttribute>(this Type type)
 
34
        {
 
35
            return type.FirstAttribute<TAttribute>(true);
 
36
        }
 
37
 
 
38
        public static TAttribute FirstAttribute<TAttribute>(this Type type, bool inherit)
 
39
        {
 
40
            var attrs = type.GetCustomAttributes(typeof(TAttribute), inherit);
 
41
            return (TAttribute)(attrs.Length > 0 ? attrs[0] : null);
 
42
        }
 
43
 
 
44
        public static TAttribute FirstAttribute<TAttribute>(this PropertyInfo propertyInfo)
 
45
        {
 
46
            return propertyInfo.FirstAttribute<TAttribute>(true);
 
47
        }
 
48
 
 
49
        public static TAttribute FirstAttribute<TAttribute>(this PropertyInfo propertyInfo, bool inherit)
 
50
        {
 
51
            var attrs = propertyInfo.GetCustomAttributes(typeof(TAttribute), inherit);
 
52
            return (TAttribute)(attrs.Length > 0 ? attrs[0] : null);
 
53
        }
 
54
 
 
55
        public static bool IsGenericType(this Type type)
 
56
        {
 
57
            while (type != null)
 
58
            {
 
59
                if (type.IsGenericType)
 
60
                    return true;
 
61
 
 
62
                type = type.BaseType;
 
63
            }
 
64
            return false;
 
65
        }
 
66
 
 
67
        public static Type FirstGenericTypeDefinition(this Type type)
 
68
        {
 
69
            while (type != null)
 
70
            {
 
71
                if (type.IsGenericType)
 
72
                    return type.GetGenericTypeDefinition();
 
73
 
 
74
                type = type.BaseType;
 
75
            }
 
76
 
 
77
            return null;
 
78
        }
 
79
 
 
80
        public static bool IsDynamic(this Assembly assembly)
 
81
        {
 
82
#if MONOTOUCH
 
83
            return false;
 
84
#else
 
85
            try
 
86
            {
 
87
                var isDyanmic = assembly is System.Reflection.Emit.AssemblyBuilder
 
88
                    || string.IsNullOrEmpty(assembly.Location);
 
89
                return isDyanmic;
 
90
            }
 
91
            catch (NotSupportedException)
 
92
            {
 
93
                //Ignore assembly.Location not supported in a dynamic assembly.
 
94
                return true;
 
95
            }
 
96
#endif
 
97
        }
 
98
 
 
99
        public static bool IsDebugBuild(this Assembly assembly)
 
100
        {
 
101
            return assembly.GetCustomAttributes(false)
 
102
                .OfType<DebuggableAttribute>()
 
103
                .Select(attr => attr.IsJITTrackingEnabled)
 
104
                .FirstOrDefault();
 
105
        }
 
106
    }
 
107
}
 
108
 
 
109
 
 
110
#if FALSE && DOTNET35
 
111
//Efficient POCO Translator from: http://www.yoda.arachsys.com/csharp/miscutil/
 
112
using System;
 
113
using System.Collections.Generic;
 
114
using System.Linq.Expressions;
 
115
using System.Reflection;
 
116
 
 
117
namespace MiscUtil.Reflection
 
118
{
 
119
    /// <summary>
 
120
    /// Generic class which copies to its target type from a source
 
121
    /// type specified in the Copy method. The types are specified
 
122
    /// separately to take advantage of type inference on generic
 
123
    /// method arguments.
 
124
    /// </summary>
 
125
    public static class PropertyCopy<TTarget> where TTarget : class, new()
 
126
    {
 
127
        /// <summary>
 
128
        /// Copies all readable properties from the source to a new instance
 
129
        /// of TTarget.
 
130
        /// </summary>
 
131
        public static TTarget CopyFrom<TSource>(TSource source) where TSource : class
 
132
        {
 
133
            return PropertyCopier<TSource>.Copy(source);
 
134
        }
 
135
 
 
136
        /// <summary>
 
137
        /// Static class to efficiently store the compiled delegate which can
 
138
        /// do the copying. We need a bit of work to ensure that exceptions are
 
139
        /// appropriately propagated, as the exception is generated at type initialization
 
140
        /// time, but we wish it to be thrown as an ArgumentException.
 
141
        /// </summary>
 
142
        private static class PropertyCopier<TSource> where TSource : class
 
143
        {
 
144
            private static readonly Func<TSource, TTarget> copier;
 
145
            private static readonly Exception initializationException;
 
146
 
 
147
            internal static TTarget Copy(TSource source)
 
148
            {
 
149
                if (initializationException != null)
 
150
                {
 
151
                    throw initializationException;
 
152
                }
 
153
                if (source == null)
 
154
                {
 
155
                    throw new ArgumentNullException("source");
 
156
                }
 
157
                return copier(source);
 
158
            }
 
159
 
 
160
            static PropertyCopier()
 
161
            {
 
162
                try
 
163
                {
 
164
                    copier = BuildCopier();
 
165
                    initializationException = null;
 
166
                }
 
167
                catch (Exception e)
 
168
                {
 
169
                    copier = null;
 
170
                    initializationException = e;
 
171
                }
 
172
            }
 
173
 
 
174
            private static Func<TSource, TTarget> BuildCopier()
 
175
            {
 
176
                ParameterExpression sourceParameter = Expression.Parameter(typeof(TSource), "source");
 
177
                var bindings = new List<MemberBinding>();
 
178
                foreach (PropertyInfo sourceProperty in typeof(TSource).GetProperties())
 
179
                {
 
180
                    if (!sourceProperty.CanRead)
 
181
                    {
 
182
                        continue;
 
183
                    }
 
184
                    PropertyInfo targetProperty = typeof(TTarget).GetProperty(sourceProperty.Name);
 
185
                    if (targetProperty == null)
 
186
                    {
 
187
                        throw new ArgumentException("Property " + sourceProperty.Name + " is not present and accessible in " + typeof(TTarget).FullName);
 
188
                    }
 
189
                    if (!targetProperty.CanWrite)
 
190
                    {
 
191
                        throw new ArgumentException("Property " + sourceProperty.Name + " is not writable in " + typeof(TTarget).FullName);
 
192
                    }
 
193
                    if (!targetProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType))
 
194
                    {
 
195
                        throw new ArgumentException("Property " + sourceProperty.Name + " has an incompatible type in " + typeof(TTarget).FullName);
 
196
                    }
 
197
                    bindings.Add(Expression.Bind(targetProperty, Expression.Property(sourceParameter, sourceProperty)));
 
198
                }
 
199
                Expression initializer = Expression.MemberInit(Expression.New(typeof(TTarget)), bindings);
 
200
                return Expression.Lambda<Func<TSource,TTarget>>(initializer, sourceParameter).Compile();
 
201
            }
 
202
        }
 
203
    }
 
204
}
 
205
#endif