19 using System.Collections.Generic;
20 using System.Reflection;
25 public partial class PandasData
27 private static DataTypeMember CreateDataTypeMember(MemberInfo member, DataTypeMember[] children =
null)
31 PropertyInfo
property =>
new PropertyMember(property, children),
32 FieldInfo field =>
new FieldMember(field, children),
33 _ =>
throw new ArgumentException($
"Member type {member.MemberType} is not supported")
41 private abstract class DataTypeMember
43 private static readonly StringBuilder _stringBuilder =
new StringBuilder();
45 private DataTypeMember _parent;
48 public MemberInfo Member {
get; }
50 public DataTypeMember[] Children {
get; }
52 public abstract bool IsProperty {
get; }
54 public abstract bool IsField {
get; }
59 public string Prefix {
get;
private set; }
61 public bool ShouldBeUnwrapped => Children !=
null && Children.Length > 0;
67 public bool IsTickLastPrice {
get; }
69 public bool IsTickProperty {
get; }
71 public DataTypeMember(MemberInfo member, DataTypeMember[] children =
null)
76 IsTickLastPrice = member == _tickLastPriceMember || member == _openInterestLastPriceMember;
77 IsTickProperty = IsProperty && member.DeclaringType == typeof(
Tick);
81 foreach (var child
in Children)
88 public void SetPrefix()
90 Prefix = Member.Name.ToLowerInvariant();
97 public string GetMemberName(
string customName =
null)
99 if (ShouldBeUnwrapped)
104 if (!
string.IsNullOrEmpty(customName))
106 return BuildMemberName(customName);
109 if (
string.IsNullOrEmpty(_name))
111 _name = BuildMemberName(GetBaseName());
117 public IEnumerable<string> GetMemberNames()
119 return GetMemberNames(
null);
122 public abstract object GetValue(
object instance);
124 public abstract Type GetMemberType();
126 public override string ToString()
128 return $
"{GetMemberType().Name} {Member.Name}";
131 private string BuildMemberName(
string baseName)
133 _stringBuilder.Clear();
134 while (_parent !=
null && _parent.ShouldBeUnwrapped)
136 _stringBuilder.Insert(0, _parent.Prefix);
137 _parent = _parent._parent;
140 _stringBuilder.Append(baseName.ToLowerInvariant());
141 return _stringBuilder.ToString();
144 private IEnumerable<string> GetMemberNames(
string parentPrefix)
147 if (ShouldBeUnwrapped)
149 var prefix = parentPrefix ??
string.Empty;
150 if (!
string.IsNullOrEmpty(Prefix))
155 foreach (var child
in Children)
157 foreach (var childName
in child.GetMemberNames(prefix))
159 yield
return childName;
165 var memberName = GetBaseName();
166 _name =
string.IsNullOrEmpty(parentPrefix) ? memberName : $
"{parentPrefix}{memberName}";
170 private string GetBaseName()
172 var baseName = Member.GetCustomAttribute<PandasColumnAttribute>()?.Name;
173 if (
string.IsNullOrEmpty(baseName))
175 baseName = Member.Name;
178 return baseName.ToLowerInvariant();
182 private class PropertyMember : DataTypeMember
184 private PropertyInfo _property;
186 public override bool IsProperty =>
true;
188 public override bool IsField =>
false;
190 public PropertyMember(PropertyInfo property, DataTypeMember[] children =
null)
191 : base(property, children)
193 _property = property;
196 public override object GetValue(
object instance)
198 return _property.GetValue(instance);
201 public override Type GetMemberType()
203 return _property.PropertyType;
207 private class FieldMember : DataTypeMember
209 private FieldInfo _field;
211 public override bool IsProperty =>
false;
213 public override bool IsField =>
true;
215 public FieldMember(FieldInfo field, DataTypeMember[] children =
null)
216 : base(field, children)
221 public override object GetValue(
object instance)
223 return _field.GetValue(instance);
226 public override Type GetMemberType()
228 return _field.FieldType;