一下代码假定ini文件格式为 [tjb.myclass];这个是类的namespace+名称 f1=v1 f2=v2类文件为 namespace tjb{ public class myclass{ public string f1{get;set;} public string f2{get;set;} } }
/// <summary> /// ini文件键值分隔符 /// </summary> private static string iniFieldSeparator = "="; /// <summary> /// ini文件的行分隔符号 /// </summary> private static string iniRowSeparator = "\n\r"; private static Regex regtype = new Regex(@"^\[(?<type>.+)\][ \t]*$", RegexOptions.Multiline | RegexOptions.Compiled); private static Regex regIgnore = new Regex(@"^([ \t]*|[ \t]*;.*)$", RegexOptions.Multiline | RegexOptions.Compiled); private static Regex regItem = new Regex(@"^(?<" + keyGName + ">[^[(?:" + iniFieldSeparator + @")]+)(?:" + iniFieldSeparator + ")?(?<=" + iniFieldSeparator + ")?(?<" + valueGName + ">.*)$", RegexOptions.Multiline | RegexOptions.Compiled); /// <summary> /// 将字符串解析为实例 /// <para>by tangjingbo at 2009-3-12 13:29</para> /// </summary> /// <param name="AttachStr">The attach STR.</param> /// <returns></returns> /// <exception cref="ArgumentException">参数错误</exception> /// <exception cref="Exception"></exception> public static Object StringToActionAttach(string AttachStr) { if (string.IsNullOrEmpty(AttachStr)) return null; List<string> rows = new List<string>(AttachStr.Split(new string[] { iniRowSeparator }, StringSplitOptions.RemoveEmptyEntries)); string type = "Undefined"; for (int i = 0, n = rows.Count; i < n; i++) { string cur = rows[i]; if (regIgnore.IsMatch(cur)) { continue; } if (regtype.IsMatch(cur)) { Match m = regtype.Match(cur); type = m.Groups["type"].Value; break; } } //根据ini文件中class类型 实例化一个对象 Object myclass = Activator.CreateInstance("xxx.dll", type); Type t = myclass.GetType(); foreach (string cur in rows) { if (regIgnore.IsMatch(cur)) continue; if (regItem.IsMatch(cur)) { Match m = regItem.Match(cur);
PropertyInfo f = t.GetProperty(m.Groups[keyGName].Value); //查找对象中此属性 if (f != null) { //设置对象的值 f.SetValue(myclass, m.Groups[valueGName].Value, null); } } } return myclass; }
反射可以创建数据原型class 这里已经有class了 如何把数据存储进class?这里不知道class的结构
[tjb.myclass];这个是类的namespace+名称
f1=v1
f2=v2类文件为
namespace tjb{
public class myclass{
public string f1{get;set;}
public string f2{get;set;}
}
}
/// <summary>
/// ini文件键值分隔符
/// </summary>
private static string iniFieldSeparator = "=";
/// <summary>
/// ini文件的行分隔符号
/// </summary>
private static string iniRowSeparator = "\n\r";
private static Regex regtype = new Regex(@"^\[(?<type>.+)\][ \t]*$", RegexOptions.Multiline | RegexOptions.Compiled);
private static Regex regIgnore = new Regex(@"^([ \t]*|[ \t]*;.*)$", RegexOptions.Multiline | RegexOptions.Compiled);
private static Regex regItem = new Regex(@"^(?<" + keyGName + ">[^[(?:" + iniFieldSeparator + @")]+)(?:" + iniFieldSeparator + ")?(?<=" + iniFieldSeparator + ")?(?<" + valueGName + ">.*)$", RegexOptions.Multiline | RegexOptions.Compiled);
/// <summary>
/// 将字符串解析为实例
/// <para>by tangjingbo at 2009-3-12 13:29</para>
/// </summary>
/// <param name="AttachStr">The attach STR.</param>
/// <returns></returns>
/// <exception cref="ArgumentException">参数错误</exception>
/// <exception cref="Exception"></exception>
public static Object StringToActionAttach(string AttachStr)
{
if (string.IsNullOrEmpty(AttachStr))
return null;
List<string> rows = new List<string>(AttachStr.Split(new string[] { iniRowSeparator }, StringSplitOptions.RemoveEmptyEntries));
string type = "Undefined";
for (int i = 0, n = rows.Count; i < n; i++)
{
string cur = rows[i];
if (regIgnore.IsMatch(cur))
{
continue;
}
if (regtype.IsMatch(cur))
{
Match m = regtype.Match(cur); type = m.Groups["type"].Value;
break;
} }
//根据ini文件中class类型 实例化一个对象
Object myclass = Activator.CreateInstance("xxx.dll", type);
Type t = myclass.GetType();
foreach (string cur in rows)
{
if (regIgnore.IsMatch(cur))
continue;
if (regItem.IsMatch(cur))
{
Match m = regItem.Match(cur);
PropertyInfo f = t.GetProperty(m.Groups[keyGName].Value);
//查找对象中此属性
if (f != null)
{
//设置对象的值
f.SetValue(myclass, m.Groups[valueGName].Value, null);
}
}
}
return myclass;
}