我猜你在里面待了很长时间......在我看来你必须开发一个 ORM,我曾经面临过同样的情况,我必须为 ATM 机制作一个 ORM。
此代码对您不起作用,因为它假定 IDataReader 但是您需要使用反射的属性映射,并且此代码就在其中。
让我知道这是否可以让您知道,因为它对重用反射类型等进行了一些优化。
我认为您需要测试一个属性是否为 Class 并使用激活器创建它并使用有效的演员阵容
if (property.PropertyType.BaseType == typeof(Enum))
{
property.SetValue(obj, (int)value);
}
else if (property.PropertyType.BaseType == typeof(Guid))
{
property.SetValue(obj, Guid.Parse(value.ToString().ToUpper()));
}
else
{
property.SetValue(obj, Convert.ChangeType(value, property.PropertyType));
}
我不知道您需要如何支持我的调用,或者如果它是固定数量,您可能只是在制作了一个静态 T Parse(this T target, string json) 方法之后,在该方法中,您基于 { 和 } 括号将 json 字符串分段为获取属性和 [ 和 ] 以获取数组。
这是代码,我将它用于我前段时间制作的 VCARD Json 解析器
/// <summary>
/// Splits the specified string in sections of open en closing characters.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="open">The opening char indicating where to start to read .</param>
/// <param name="close">The close char, indicating the part where should stop reading.</param>
/// <returns>IReadOnlyList<System.String>.</returns>
/// <exception cref="System.ArgumentNullException">text</exception>
/// <exception cref="ArgumentNullException">Will throw an exception if the string that needs to be split is null or empty</exception>
public static IReadOnlyList<string> Split(this string text, char open, char close)
{
if (text is null)
{
throw new ArgumentNullException(nameof(text));
}
var counted = 0;
var result = new List<string>();
var sb = new StringBuilder();
foreach (char c in text)
{
if (c == open)
{
if (counted != 0)
sb.Append(c);
counted++;
continue;
}
if (c == close)
{
counted--;
if (counted != 0)
sb.Append(c);
continue;
}
if (counted > 0)
{
sb.Append(c);
}
else if (counted == 0 && sb.Length > 0)
{
result.Add(sb.ToString());
sb.Clear();
}
}
return result;
}
这是我必须制作的完整映射器,您可以在其中看到提到的反射
class Mapper
{
ConcurrentDictionary<Type, PropertyInfo[]> _properties = new ConcurrentDictionary<Type, PropertyInfo[]>();
ConcurrentDictionary<string, List<string>> _fieldNames = new ConcurrentDictionary<string, List<string>>();
/// <summary>
/// Maps the specified reader to a given class. the reader must contain all properties of the type provided.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reader">The reader.</param>
/// <returns></returns>
public IEnumerable<T> Map<T>(SqlDataReader reader)
{
var result = new List<T>();
if (!reader.HasRows)
return result;
var type = typeof(T);
if (!_properties.TryGetValue(type, out PropertyInfo[] prop))
{
prop = type.GetProperties();
_properties.TryAdd(type, prop);
}
if (!_fieldNames.TryGetValue(type.Name, out List<string> fieldNames))
{
var names = new List<string>(reader.FieldCount);
for (int i = 0; i < reader.FieldCount; i++)
{
names.Add(reader.GetName(i));
}
fieldNames = names;
_fieldNames.TryAdd(type.Name, fieldNames);
}
while (reader.Read())
{
var obj = Activator.CreateInstance<T>();
foreach (var property in prop)
{
if (fieldNames.Contains(property.Name))
{
var value = reader[property.Name];
if (value == DBNull.Value)
continue;
if (property.PropertyType.BaseType == typeof(Enum))
{
property.SetValue(obj, (int)value);
}
else if (property.PropertyType.BaseType == typeof(Guid))
{
property.SetValue(obj, Guid.Parse(value.ToString().ToUpper()));
}
else
{
property.SetValue(obj, Convert.ChangeType(value, property.PropertyType));
}
}
}
result.Add(obj);
}
return result;
}
public IEnumerable<T> Map<T,Y>(SqlDataReader reader,Y owner)
{
var result = new List<T>();
if (!reader.HasRows)
return result;
var type = typeof(T);
if (!_properties.TryGetValue(type, out PropertyInfo[] prop))
{
prop = type.GetProperties();
_properties.TryAdd(type, prop);
}
if (!_fieldNames.TryGetValue(type.Name, out List<string> fieldNames))
{
var names = new List<string>(reader.FieldCount);
for (int i = 0; i < reader.FieldCount; i++)
{
names.Add(reader.GetName(i));
}
fieldNames = names;
_fieldNames.TryAdd(type.Name, fieldNames);
}
while (reader.Read())
{
var obj = Activator.CreateInstance<T>();
foreach (var property in prop)
{
if (property.PropertyType == typeof(Y))
{
property.SetValue(obj, owner);
continue;
}
if (fieldNames.Contains(property.Name))
{
var value = reader[property.Name];
if (value == DBNull.Value)
continue;
if (property.PropertyType.BaseType == typeof(Enum))
{
property.SetValue(obj, (int)value);
}
else
{
property.SetValue(obj, Convert.ChangeType(value, property.PropertyType));
}
}
}
result.Add(obj);
}
return result;
}
/// <summary>
/// Maps the specified reader to a given class. the reader must contain all properties of the type provided.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reader">The reader.</param>
/// <returns></returns>
public T MapOne<T>(SqlDataReader reader)
{
if (!reader.HasRows)
return default;
var type = typeof(T);
if (!_properties.TryGetValue(type, out PropertyInfo[] prop))
{
prop = type.GetProperties();
_properties.TryAdd(type, prop);
}
if (!_fieldNames.TryGetValue(type.Name, out List<string> fieldNames))
{
var names = new List<string>(reader.FieldCount);
for (int i = 0; i < reader.FieldCount; i++)
{
names.Add(reader.GetName(i));
}
fieldNames = names;
_fieldNames.TryAdd(type.Name, fieldNames);
}
if (reader.Read())
{
var obj = Activator.CreateInstance<T>();
foreach (var property in prop)
{
if (fieldNames.Contains(property.Name))
property.SetValue(obj, reader[property.Name]);
}
return obj;
}
else
{
return default;
}
}
/// <summary>
/// Maps the specified reader to a given class. the reader must contain all properties of the type provided.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reader">The reader.</param>
/// <returns></returns>
public IEnumerable<T> Map<T>(SqlDataReader reader, object[] args)
{
var result = new List<T>();
if (!reader.HasRows)
return result;
var type = typeof(T);
if (!_properties.TryGetValue(type, out PropertyInfo[] prop))
{
prop = type.GetProperties();
_properties.TryAdd(type, prop);
}
if (!_fieldNames.TryGetValue(type.Name, out List<string> fieldNames))
{
var names = new List<string>(reader.FieldCount);
for (int i = 0; i < reader.FieldCount; i++)
{
names.Add(reader.GetName(i));
}
fieldNames = names;
_fieldNames.TryAdd(type.Name, fieldNames);
}
while (reader.Read())
{
var obj = (T)Activator.CreateInstance(type, args);
foreach (var property in prop)
{
if (fieldNames.Contains(property.Name))
property.SetValue(obj, reader[property.Name]);
}
result.Add(obj);
}
return result;
}
}