forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeExtensions.cs
More file actions
40 lines (35 loc) · 1.26 KB
/
TypeExtensions.cs
File metadata and controls
40 lines (35 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Reflection;
namespace ServiceStack.Common
{
public static class TypeExtensions
{
private static readonly Dictionary<Type, List<string>> TypePropertyNamesMap = new Dictionary<Type, List<string>>();
public static List<string> GetPropertyNames(this Type type)
{
lock (TypePropertyNamesMap)
{
List<string> propertyNames;
if (!TypePropertyNamesMap.TryGetValue(type, out propertyNames))
{
propertyNames = Extensions.EnumerableExtensions.ConvertAll(type.GetProperties(), x => x.Name);
TypePropertyNamesMap[type] = propertyNames;
}
return propertyNames;
}
}
public static List<T> ToAttributes<T>(this Type type) where T : Attribute
{
return type.GetCustomAttributes(typeof(T), true).SafeConvertAll(x => (T)x);
}
#if !SILVERLIGHT
public static string GetAssemblyPath(this Type source)
{
var assemblyUri =
new Uri(source.Assembly.EscapedCodeBase);
return assemblyUri.LocalPath;
}
#endif
}
}