I am working on a .NET project that needs to take a plaintext value, and, given that value, instantiate a specific object type designed to store and work with it.
For example:
Say I am given a file containing the following three lines of text:
Role|Manager
Company|ComputerCompany
Age|27
Furthermore, I have defined the following class structure:
public abstract class Field()
{
public string FieldDataType;
public abstract void CustomBehavior();
}
public class StringField : Field
{
public string value;
public StringField ()
{
FieldDataType = "String";
}
// implement CustomBehavior() here
}
public class IntField : Field
{
public int value;
public IntField ()
{
FieldDataType = "Int";
}
// implement CustomBehavior() here
}
...
When my file parser reads in the line "Role|Manager" I want to use the text "Role" to create an instance of the StringField
type using reflection and store the value "Manager" in it.
Similarly when I read in the line "Company|ComputerCompany" I want to use the text "Company" to create an instance of the StringField
type, and when I read in the line "Age|27" I want to use the text "Age" create an instance of the IntField
type.
Is there a way, perhaps using attributes, to map a known list of field names such as "Age" "Company" and "Role" to a set of custom qualified type names?
Aucun commentaire:
Enregistrer un commentaire