C# 使用属性将字符串拆分为对象是否存在性能风险?
对于我的工作,我创建了一个C# 使用属性将字符串拆分为对象是否存在性能风险?,c#,string,object,attributes,split,C#,String,Object,Attributes,Split,对于我的工作,我创建了一个ListStringSerializer,将字符串拆分为一个对象。 我使用属性为解析器创建模板。通过反射,我将字符串(“a:b:c:d,d:c:b:a”)转换为对象 使用反射时是否存在性能风险 是否有更好的解决方案将字符串拆分为对象 源代码: public class ListStringSerializer { public IEnumerable<T> Deserialize<T>(string inputStr) where T
ListStringSerializer
,将字符串拆分为一个对象。
我使用属性为解析器创建模板。通过反射,我将字符串(“a:b:c:d,d:c:b:a”)转换为对象
- 使用反射时是否存在性能风险李>
- 是否有更好的解决方案将字符串拆分为对象李>
public class ListStringSerializer
{
public IEnumerable<T> Deserialize<T>(string inputStr) where T : new()
{
if (string.IsNullOrEmpty(inputStr))
yield return new T();
var obj = new T();
var classAttrs = obj.GetType().GetCustomAttributes(typeof(TemplateParserAttribute), true).Cast<TemplateParserAttribute>();
if (!classAttrs.Any())
yield return new T();
var rows = Regex.Split(inputStr, classAttrs.First().SplitRow);
foreach (var rowData in rows.Select(row => Regex.Split(row, classAttrs.First().SplitData)))
{
yield return GetObjectValue<T>(rowData);
}
}
private static T GetObjectValue<T>(string[] rowData) where T : new()
{
var obj = new T();
foreach (var property in
from prop in obj.GetType().GetProperties()
let attributes =
prop.GetCustomAttributes(typeof (ParseFieldAttribute), true).Cast<ParseFieldAttribute>().
OrderBy(p => p.Position)
where attributes.Any()
select new {PropertyInfo = prop, Attribute = attributes.First()})
{
if (rowData.Length <= property.Attribute.Position - 1) continue;
var value = rowData[property.Attribute.Position - 1];
if (property.Attribute.MergeLastPart)
{
for (var i = (property.Attribute.Position - 1); i < rowData.Length; i++)
{
value += " " + rowData[i];
}
}
if (property.Attribute.SplitValue != null && property.Attribute.ReplaceValue != null)
{
value.Replace(property.Attribute.SplitValue, property.Attribute.ReplaceValue);
}
property.PropertyInfo.SetValue(obj, value, null);
}
return obj;
}
}
public class ParseFieldAttribute : Attribute
{
public int Position { get; set; }
public IDictionary StrConvert { get; set; }
public bool MergeLastPart { get; set; }
public string SplitValue { get; set; }
public string ReplaceValue { get; set; }
public ParseFieldAttribute(int position)
: this(position, false)
{}
public ParseFieldAttribute(int position, bool mergeLastPart) : this( position, mergeLastPart, null, null)
{}
public ParseFieldAttribute(int position, bool mergeLastPart, string splitValue, string replaceValue )
{
Position = position;
MergeLastPart = mergeLastPart;
SplitValue = splitValue;
ReplaceValue = replaceValue;
}
}
public class TemplateParserAttribute : Attribute
{
public int TotalPosition { get; set; }
public string SplitRow { get; set; }
public string SplitData { get; set; }
public TemplateParserAttribute(int totalposition, string splitRow, string splitData)
{
TotalPosition = totalposition;
SplitData = splitData;
SplitRow = splitRow;
}
}
[TemplateParser(1,",",":")]
public class ReaccomReseating
{
[ParseField(1)]
public string Group { get; set; }
[ParseField(2)]
public string FirstName{ get; set; }
[ParseField(3)]
public string LastName { get; set; }
[ParseField(4)]
public string City { get; set; }
}
公共类ListStringSerializer
{
公共IEnumerable反序列化(字符串inputStr),其中T:new()
{
if(string.IsNullOrEmpty(inputStr))
新T()的收益率;
var obj=新的T();
var classAttrs=obj.GetType().GetCustomAttributes(typeof(TemplateParserAttribute),true.Cast();
如果(!classAttrs.Any())
新T()的收益率;
var rows=Regex.Split(inputStr,classAttrs.First().SplitRow);
foreach(var rowData in rows.Select(row=>Regex.Split(row,classAttrs.First().SplitData)))
{
产生返回GetObjectValue(rowData);
}
}
私有静态T GetObjectValue(字符串[]rowData),其中T:new()
{
var obj=新的T();
foreach(中的var属性)
从obj.GetType().GetProperties()中的prop
let属性=
prop.GetCustomAttributes(typeof(ParseFieldAttribute),true).Cast()。
订货人(p=>p.Position)
where attributes.Any()
选择新{PropertyInfo=prop,Attribute=attributes.First()})
{
如果(rowData.Length始终存在性能问题的风险或潜在风险,但只有分析才能意识到这一潜在风险。如果您真的不知道,我的建议是将实现从调用它的人那里抽象出来,这样,如果性能差,您可以在对其他代码影响最小的情况下重新实现。感谢您的支持回复