Tanoda
LeapGraphicTagAttribute.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Linq;
11using System.Collections.Generic;
12
14
15 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
16 public class LeapGraphicTagAttribute : Attribute {
17 private static Dictionary<Type, LeapGraphicTagAttribute> _tagCache = new Dictionary<Type, LeapGraphicTagAttribute>();
18 private static Dictionary<string, Type> _stringTypeCache = new Dictionary<string, Type>();
19
20 public readonly string name;
21 public readonly int order;
22
23 public LeapGraphicTagAttribute(string name, int order = 0) {
24 this.name = name;
25 this.order = order;
26 }
27
28 public static string GetTagName(Type type) {
29 var tag = GetTag(type);
30 return tag == null ? type.Name : tag.name;
31 }
32
33 public static string GetTagName(string typeName) {
34 var tag = GetTag(typeName);
35 return tag == null ? typeName : tag.name;
36 }
37
38 public static LeapGraphicTagAttribute GetTag(Type type) {
40 if (!_tagCache.TryGetValue(type, out tag)) {
41 object[] attributes = type.GetCustomAttributes(typeof(LeapGraphicTagAttribute), inherit: true);
42 if (attributes.Length == 1) {
43 tag = attributes[0] as LeapGraphicTagAttribute;
44 }
45 _tagCache[type] = tag;
46 }
47
48 return tag;
49 }
50
51 public static LeapGraphicTagAttribute GetTag(string typeName) {
52 Type type;
53 if (!_stringTypeCache.TryGetValue(typeName, out type)) {
54 type = typeof(LeapGraphicTagAttribute).Assembly.GetTypes().FirstOrDefault(t => t.Name == typeName);
55 _stringTypeCache[typeName] = type;
56 }
57
58 if (type == null) {
59 return null;
60 } else {
61 return GetTag(type);
62 }
63 }
64 }
65}
static LeapGraphicTagAttribute GetTag(string typeName)
static LeapGraphicTagAttribute GetTag(Type type)