This is a working in progress, I need to create an object with info got from a xml file - a metadata.
Here is the metadata, the text to construct the object:
Creator:Gustavo Jesien Pinent
Subject:How to play Rescue Space Miners
Description:Multilanguage text structured into fields
Publisher:Virtualis
Date:2020-05-06
Type:MultiLanguageSimpleText
Format:UTF-8,URI
Identifier:Instructions1
Language:pt=Português,en=English,es=Español
Rights:GPL3.0```
This is the code:
```csharp
public class DCPMetadata
{
private Dictionary<string, string> FieldTypes = new Dictionary<string, string>()
{
{ "Title", "string" },
{ "Creator", "string" },
{ "Subject", "string" },
{ "Description", "object" },
{ "Publisher", "string" },
{ "Contributer", "string" },
{ "Date", "DateTime" },
{ "Type", "string" },
{ "Format", "string" },
{ "Identifyer", "string" },
{ "Source", "string" },
{ "Language", "Dictionary" },
{ "Relation", "string" },
{ "Coverage", "string" },
{ "Rights", "string" }
};
public string Title;
public string Creator;
public string Subject;
public object Description;
public string Publisher;
public string Contributer;
public DateTime Date;
public string Type;
public string Format;
public string Identifyer;
public string Source;
public Dictionary<string, string> Language;
public string Relation;
public string Coverage;
public string Rights;
public List<string> Errors { get; private set; }
public DCPMetadata(string text)
{
this.Errors = new List<string>();
string[] Lines = text.Split('\n');
foreach (string line in Lines)
{
string l = line.Trim();
string[] E = l.Split(':');
if (this.FieldTypes.ContainsKey(E[0]))
{
string type = this.FieldTypes[E[0]];
switch (type)
{
case "object":
// not implemented yet...
break;
case "Date":
this[E[0]] = System.DateTime.Parse(E[1]);
break;
case "Dictionary":
string[] pairs = E[1].Split(',');
Dictionary<string, string> D = new Dictionary<string, string>();
foreach (string pair in pairs)
{
string[] T = pair.Split('=');
D.Add(T[0], T[1]);
}
this[E[0]] = D;
break;
case "string":
this[E[0]] = E[1];
break;
}
}
else
{
this.Errors.Add("DCPMetadata construction ERROR: field '" + E[0] + "' non standard.");
}
}
}
public object this[string propertyName] // need System.Reflection
{
get
{
// probably faster without reflection:
// like: return Properties.Settings.Default.PropertyValues[propertyName]
// instead of the following
Type myType = GetType();
PropertyInfo myPropInfo = myType.GetProperty(propertyName);
if (myPropInfo != null) return myPropInfo.GetValue(this, null);
else return null;
}
set
{
Type myType = GetType();
PropertyInfo myPropInfo = myType.GetProperty(propertyName);
if(myPropInfo != null) myPropInfo.SetValue(this, value, null);
}
}
}
I’d pick the code that set the property by string around the web but can’t make it work. myPropInfo is always null, if I remove the ‘if’, I got a Null Reference Error. Why?