BadScript 2
Loading...
Searching...
No Matches
BadReflectedMemberTable.cs
Go to the documentation of this file.
1using System.Reflection;
2
8
13
19{
23 private static readonly Dictionary<Type, BadReflectedMemberTable> s_TableCache =
24 new Dictionary<Type, BadReflectedMemberTable>();
25
29 private readonly Dictionary<string, BadReflectedMember> m_Members;
30
35 private BadReflectedMemberTable(Dictionary<string, BadReflectedMember> members)
36 {
37 m_Members = members;
38 }
39
43 public IEnumerable<string> MemberNames => m_Members.Keys;
44
50 public bool Contains(string name)
51 {
52 return m_Members.ContainsKey(name);
53 }
54
62 public BadObjectReference GetMember(object instance, string name)
63 {
64 if (m_Members.TryGetValue(name, out BadReflectedMember member))
65 {
66 return member.IsReadOnly
67 ? BadObjectReference.Make(name, () => member.Get(instance))
68 : BadObjectReference.Make(name, () => member.Get(instance), (o, _) => member.Set(instance, o));
69 }
70
71 throw new BadRuntimeException("Member " + name + " not found");
72 }
73
80 {
81 return Create(typeof(T));
82 }
83
89 public static BadReflectedMemberTable Create(Type t)
90 {
91 if (s_TableCache.TryGetValue(t, out BadReflectedMemberTable? value))
92 {
93 return value;
94 }
95
96 BadLogger.Log($"Creating Member Table for {t.Name}", "BadReflection");
98 s_TableCache[t] = table;
99
100 return table;
101 }
102
109 {
110 Dictionary<string, BadReflectedMember> members = new Dictionary<string, BadReflectedMember>();
111
112 if (t.IsArray)
113 {
114 members.Add(BadStaticKeys.ARRAY_ACCESS_OPERATOR_NAME, new BadReflectedMethod(t.GetMethod("Get")!));
115 }
116
117 foreach (MemberInfo info in t.GetMembers())
118 {
119 if (info is FieldInfo field && !members.ContainsKey(field.Name))
120 {
121 members.Add(field.Name, new BadReflectedField(field));
122 }
123 else if (info is PropertyInfo property)
124 {
125 if (property.Name == "Item" && property.GetIndexParameters().Length > 0)
126 {
127 if (!members.ContainsKey(BadStaticKeys.ARRAY_ACCESS_OPERATOR_NAME))
128 {
129 members.Add(BadStaticKeys.ARRAY_ACCESS_OPERATOR_NAME, new BadReflectedMethod(property.GetMethod));
130 }
131 }
132 else if (!members.ContainsKey(property.Name))
133 {
134 members.Add(property.Name, new BadReflectedProperty(property));
135 }
136 }
137 else if (info is MethodInfo method)
138 {
139 if (method.Name == "GetEnumerator")
140 {
141 members["GetEnumerator"] = new BadReflectedEnumeratorMethod(method);
142 }
143 else if (members.ContainsKey(method.Name) && members[method.Name] is BadReflectedMethod m)
144 {
145 m.AddMethod(method);
146 }
147 else if (!members.ContainsKey(method.Name))
148 {
149 members.Add(method.Name, new BadReflectedMethod(method));
150 }
151 }
152 }
153
154 return new BadReflectedMemberTable(members);
155 }
156}
Contains Static Data for the BadScript Language.
Public facing interface for a logger.
Definition BadLogger.cs:7
static void Log(string message)
Writes a Log to the Message Handler.
Definition BadLogger.cs:26
Implements a Member Table for a specific Type, including a Caching Mechanism for Reflected Members to...
readonly Dictionary< string, BadReflectedMember > m_Members
The Reflected Members.
static BadReflectedMemberTable Create(Type t)
Creates a new Member Table for the given Type.
static readonly Dictionary< Type, BadReflectedMemberTable > s_TableCache
The Member Table Cache.
BadReflectedMemberTable(Dictionary< string, BadReflectedMember > members)
Creates a new BadReflectedMemberTable.
static BadReflectedMemberTable Create< T >()
Creates a new Member Table for the given Type.
bool Contains(string name)
Returns true if the Member Table contains the given Member.
BadObjectReference GetMember(object instance, string name)
Returns a reference to the Member with the given Name.
static BadReflectedMemberTable CreateInternal(Type t)
Creates a new Member Table for the given Type.
Implements the base functionality for a BadScript Reference.
static BadObjectReference Make(string refText, Func< BadObject > getter, Action< BadObject, BadPropertyInfo?>? setter=null, Action? delete=null)
Creates a new Reference Object.
Contains Logging system for the BadScript Runtime.
Definition BadLog.cs:6
Contains Shared Data Structures and Functionality.
Contains the Error Objects for the BadScript2 Language.
Contains the Member Classes for Reflection Objects.
Contains the Classes for Reflection Objects.
Contains the Runtime Objects.
Definition BadArray.cs:10