Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
oven425 committed Jan 27, 2022
1 parent 7492131 commit bc36cee
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 150 deletions.
82 changes: 53 additions & 29 deletions ConsoleApp_IniT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,71 @@ class Program
{
static void Main(string[] args)
{
Setting setting = new Setting();
Setting setting = new Setting()
{
IP = "127.0.0.1",
Port = 88,
TestItems1 = new List<TestItem>()
{
new TestItem(){Exe = "A.exe", Command="qq"},
new TestItem(){Exe="B.bat", Command=""},
new TestItem(){Exe="taskmgr.exe"}
}
};

setting.Ftp_1 = new RemoteSetting() { IP = "127.0.0.100", Port = 50, Account = "Allen", Password = "123" };
setting.FTP_Log = new RemoteSetting() { IP = "192.168.10.100", Port = 96, Account = "Julia", Password = "456" };
setting.FTP_Backup = new RemoteSetting() { IP = "168.55.55.55", Port = 12, Account = "David", Password = "789" };
//setting.Ftp_1 = new RemoteSetting() { IP = "127.0.0.100", Port = 50, Account = "Allen", Password = "123" };
//setting.FTP_Log = new RemoteSetting() { IP = "192.168.10.100", Port = 96, Account = "Julia", Password = "456" };
//setting.FTP_Backup = new RemoteSetting() { IP = "168.55.55.55", Port = 12, Account = "David", Password = "789" };

setting.TestItems1 = new List<TestItem>()
{
new TestItem(){Exe = "A.exe", Command="qq"},
new TestItem(){Exe="B.bat", Command=""},
new TestItem(){Exe="taskmgr.exe"}
};
//setting.TestItems1 = new List<TestItem>()
//{
// new TestItem(){Exe = "A.exe", Command="qq"},
// new TestItem(){Exe="B.bat", Command=""},
// new TestItem(){Exe="taskmgr.exe"}
//};

//setting.TestItems2 = new List<TestItem>()
//{
// new TestItem(){Exe = "A2.exe", Command="qq2"},
// new TestItem(){Exe="B2.bat", Command=""},
// new TestItem(){Exe="taskmgr2.exe"}
//};


string ini_str = IniConvert.SerializeObject(setting);
File.WriteAllText("setting.ini", ini_str);
var inides = IniConvert.DeserializeObject<Setting>(ini_str);

}

}

[QSoft.Ini.IniSection(DefaultSection = "General")]
public class Setting
{
//public string Title { set; get; } = "Plan1";
//public DateTime ModifyTime { set; get; } = DateTime.Now;
//public TimeSpan TimeOut { set; get; } = TimeSpan.FromSeconds(30);

public RemoteSetting Ftp_1 { set; get; }
[IniSectionKey(Key ="FTP_2")]
public RemoteSetting FTP_Log { set; get; }
[IniSection()]
public RemoteSetting FTP_Backup { set; get; }

[IniArray]
public List<TestItem> TestItems1 { set; get; }
public class Setting
{
public string IP { set; get; }
public int Port { set; get; }
[IniArray(Name ="Test")]
public List<TestItem> TestItems1 { set; get; }
}

//[IniArray]
//public List<TestItem> TestItems2 { set; get; }
}
//[QSoft.Ini.IniSection()]
//public class Setting
//{
// public string Title { set; get; } = "Plan1";
// public DateTime ModifyTime { set; get; } = DateTime.Now;
// public TimeSpan TimeOut { set; get; } = TimeSpan.FromSeconds(30);

// public RemoteSetting Ftp_1 { set; get; }
// [IniSectionKey(Key ="FTP_2")]
// public RemoteSetting FTP_Log { set; get; }
// [IniSection()]
// public RemoteSetting FTP_Backup { set; get; }

// [IniArray]
// public List<TestItem> TestItems1 { set; get; }

// [IniArray(Name="Demo", BaseIndex =10)]
// public List<TestItem> TestItems2 { set; get; }
//}

public class TestItem
{
Expand Down
49 changes: 32 additions & 17 deletions Ini-Serializer/IniConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ namespace QSoft.Ini
[AttributeUsage(AttributeTargets.Class| AttributeTargets.Property, Inherited = false)]
public class IniSection : Attribute
{
[Obsolete("Please use IniConvert")]
public string DefaultSection { set; get; }
public string Name { set; get; }
}


Expand All @@ -40,16 +42,28 @@ public class IniArray : Attribute
public string Name { set; get; }
}



public static class IniConvert
{
static string GetSectionName(this Type type)
{
var section = type.GetCustomAttributes(true).FirstOrDefault(x => x is IniSection) as IniSection;
return section?.Name ?? type.Name;
}

static string GetSectionKeyName(this PropertyInfo type)
{
var section = type.GetCustomAttributes(true).FirstOrDefault(x => x is IniSectionKey) as IniSectionKey;
return section?.Key ?? type.Name;
}

static public string SerializeObject(object obj)
{
var section1 = obj.GetType().GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(IniSection)) as IniSection;
Dictionary<string, Dictionary<string, string>> sections = new Dictionary<string, Dictionary<string, string>>();
List<string> sectionlist = new List<string>();

Pack(obj, section1 == null ? obj.GetType().Name : section1.DefaultSection, sections, sectionlist);
Pack(obj, obj.GetType().GetSectionName(), sections, sectionlist);

StringBuilder strb = new StringBuilder();
foreach (var section in sectionlist)
Expand Down Expand Up @@ -88,16 +102,18 @@ static string ToXml(object obj)
return str;
}



static void Pack(object obj, string section_name, Dictionary<string, Dictionary<string, string>> sections, List<string> sectionlist)
{
if (sections.ContainsKey(section_name) == false)
{
sectionlist.Add(section_name);
sections[section_name] = new Dictionary<string, string>();
}
var pps = obj.GetType().GetProperties().Where(x => x.CanRead == true)
var pps = obj.GetType().GetProperties()
.Select(x => new { attrs = x.GetCustomAttributes(true), property = x, typecode = Type.GetTypeCode(x.PropertyType) })
.Where(x => x.attrs.Any(y => y.GetType() == typeof(IniIgnore)) == false);
.Where(x => x.property.CanRead == true && x.attrs.Any(y => y is IniIgnore) == false);
foreach (var pp in pps)
{
switch (pp.typecode)
Expand All @@ -114,7 +130,7 @@ static void Pack(object obj, string section_name, Dictionary<string, Dictionary<
{
continue;
}
var iniarray = pp.attrs.FirstOrDefault(x => x.GetType() == typeof(IniArray)) as IniArray;
var iniarray = pp.attrs.FirstOrDefault(x => x is IniArray) as IniArray;
if (iniarray == null)
{
string xml = ToXml(pp.property.GetValue(obj, null));
Expand Down Expand Up @@ -148,9 +164,9 @@ static void Pack(object obj, string section_name, Dictionary<string, Dictionary<
if (subobj_section != null)
{
string sub_name = pp.property.Name;
if(string.IsNullOrEmpty(subobj_section.DefaultSection) == false)
if(string.IsNullOrEmpty(subobj_section.Name) == false)
{
sub_name = subobj_section.DefaultSection;
sub_name = subobj_section.Name;
}
Pack(subobj, sub_name, sections, sectionlist);
}
Expand All @@ -174,7 +190,7 @@ static void Pack(object obj, string section_name, Dictionary<string, Dictionary<
var subobj = pp.property.GetValue(obj, null);
if(subobj != null)
{
sections[section_name][pp.property.Name] = $"{pp.property.GetValue(obj, null)}";
sections[section_name][pp.property.GetSectionKeyName()] = $"{pp.property.GetValue(obj, null)}";
}
}
break;
Expand All @@ -187,12 +203,11 @@ static public T DeserializeObject<T>(string ini) where T : class
var dics = Parse(ini);
var type = typeof(T);
var oi = type.GetCustomAttributes(true).FirstOrDefault(x => x == typeof(IniSection));
var attr_section = (type.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(IniSection)) as IniSection)?.DefaultSection;
var section_name = attr_section ?? type.Name;
var section_name = type.GetSectionName();
if (dics.ContainsKey(section_name) == true)
{
var obj = Activator.CreateInstance<T>();
UnPack(dics, obj, attr_section ?? type.Name);
UnPack(dics, obj, section_name);
return obj;
}

Expand Down Expand Up @@ -221,9 +236,9 @@ static void UnPack(Dictionary<string, Dictionary<string, string>> ini, object ob
foreach (var pp in pps)
{
string str = null;
if (ini[section_name].ContainsKey(pp.x.Name) == true)
if (ini[section_name].ContainsKey(pp.x.GetSectionKeyName()) == true)
{
str = ini[section_name][pp.x.Name];
str = ini[section_name][pp.x.GetSectionKeyName()];
}

switch (pp.typecode)
Expand All @@ -244,7 +259,7 @@ static void UnPack(Dictionary<string, Dictionary<string, string>> ini, object ob
pp.x.SetValue(obj, ienumable, null);
}
MethodInfo method = ienumable.GetType().GetMethod("Add");
var iniarray = pp.attrs.FirstOrDefault(x => x.GetType() == typeof(IniArray)) as IniArray;
var iniarray = pp.attrs.FirstOrDefault(x => x is IniArray) as IniArray;
if (iniarray == null)
{
var ddd = FromXml(str, pp.property);
Expand Down Expand Up @@ -281,15 +296,15 @@ static void UnPack(Dictionary<string, Dictionary<string, string>> ini, object ob
}
else
{
var subobj_section = pp.attrs.FirstOrDefault(x => x.GetType() == typeof(IniSection)) as IniSection;
var subobj_section = pp.attrs.FirstOrDefault(x => x is IniSection) as IniSection;

if (subobj_section != null)
{

var subobj_name = pp.x.Name;
if(string.IsNullOrEmpty(subobj_section.DefaultSection) == false)
if(string.IsNullOrEmpty(subobj_section.Name) == false)
{
subobj_name = subobj_section.DefaultSection;
subobj_name = subobj_section.Name;
}
var subobj = Activator.CreateInstance(pp.property);
UnPack(ini, subobj, subobj_name);
Expand Down
Binary file modified Nuget/48720666811_25b23b2ed5_o.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Nuget/QSoft.Ini.1.1.0.nupkg
Binary file not shown.
119 changes: 15 additions & 104 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,105 +1,16 @@
# ini-convert
ini serialization like json xml serialization
## ini serialize
### object to ini file
1. First defined class
```csharp
[IniSection(DefaultSection = "General")]
public class CSetting
### Simple example
```c#
Setting setting = new Setting()
{
[QSoft.Ini.IniSectionKey(Section = "Auth", Key = "Account")]
public string Name { set; get; }

[QSoft.Ini.IniSectionKey(Section = "Auth", Key = "Password")]
public string Password { set; get; }

[QSoft.Ini.IniSectionKey(Ignore = true)]
public int MaxCount { set; get; } = 100;

[QSoft.Ini.IniSectionKey(Key = "Min")]
public int MinCount { set; get; } = 10;

[QSoft.Ini.IniSectionKey(Key = "Duration")]
public TimeSpan Time { set; get; }
}
```
2. Serialize
```csharp
CSetting inifile = new CSetting();
inifile.MaxCount = 1000;
inifile.MinCount = 1;
inifile.Name = "account";
inifile.Password = "password";
QSoft.Ini.IniSerializer ini = new QSoft.Ini.IniSerializer();
ini.Serialize(inifile, "test1.ini");
```

```ini
[Auth]
Account=account
Password=password
[CSetting]
Min=1
Duration=10.09:08:07
```
3. Deserialize
```csharp
CSetting inifile1 = new CSetting();
QSoft.Ini.IniSerializer ini = new QSoft.Ini.IniSerializer();
ini.Deserialize(inifile1, "test1.ini");
```

## Legacy
### write data to ini file
```csharp
QSoft.Ini.IniSerializer ini = new QSoft.Ini.IniSerializer();
ini.Write("test", "bool", true, "test.ini");
ini.Write("test", "int", int.MaxValue, "test.ini");
ini.Write("test", "string", "write ini", "test.ini");
ini.Write("test", "Now", DateTime.Now, "test.ini");
ini.Write("test", "TimeSpan", new TimeSpan(1, 2, 3, 4), "test.ini");
```

```ini
[test]
bool=True
int=2147483647
string=write ini
Now=2020-12-28 15:15:12.561
TimeSpan=1.02:03:04
```
### read data from ini file
```csharp
QSoft.Ini.IniSerializer ini = new QSoft.Ini.IniSerializer();
bool a = ini.Read<bool>("test", "bool", "test.ini");
int b = ini.Read<int>("test", "int", "test.ini");
string c = ini.Read<string>("test", "string", "test.ini");
DateTime d = ini.Read<DateTime>("test", "Now", "test.ini");
TimeSpan e = ini.Read<TimeSpan>("test", "TimeSpan", "test.ini");
```
## Advanced
If write type is Class,List or Array,
auto transform object to xml and write ini

write sample code
```csharp
QSoft.Ini.IniSerializer ini = new QSoft.Ini.IniSerializer();
ini.Write("test", "List", new List<int>() { 9, 8, 7 }, "test.ini");
ini.Write("test", "Array", new int[] { 1, 2, 3 }, "test.ini");
ini.Write("test", "CTest", new CTest() { A = "答案A", B = 101, Test1 = new CTest_1() { A1 = "答案B", B1 = 202 } }, "test.ini");
```

```ini
[test]
List=<?xml version="1.0" encoding="utf-8"?><ArrayOfInt xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><int>9</int><int>8</int><int>7</int></ArrayOfInt>
Array=<?xml version="1.0" encoding="utf-8"?><ArrayOfInt xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><int>1</int><int>2</int><int>3</int></ArrayOfInt>
CTest=<?xml version="1.0" encoding="utf-8"?><CTest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><A>答案A</A><B>101</B><Test1><A1>答案B</A1><B1>202</B1></Test1></CTest>
```

read sample code
```csharp
QSoft.Ini.IniSerializer ini = new QSoft.Ini.IniSerializer();
List<int> f = ini.Read<List<int>>("test", "List", "test.ini");
int[] g = ini.Read<int[]>("test", "Array", "test.ini");
CTest h = ini.Read<CTest>("test", "CTest", "test.ini");
```
IP = "127.0.0.1",
Port = 88
};
string ini_str = IniConvert.SerializeObject(setting);
/*
[Setting]
IP=127.0.0.1
Port=88
*/
var deserialize = IniConvert.DeserializeObject<Setting>(ini_str);
```
[More](https://github.com/oven425/QSoft.Ini/wiki/Quick-Start)

0 comments on commit bc36cee

Please sign in to comment.