Parse To was a project first conceptualized as a way to commonly handle nulls but soon moved into a generic Parser to take an object variable and turn it into a generically passed type, a built in default for the type or a passed default for the type.
It does this by using an IParse<> interface that creates a handler for the type you wish to cast to or resturns an Exception if the type has not IParse<> impelmentation
To build custom parsable type inherit a handler class or actual parsable object class from the IParse<[ObjectClassName]>(Must have a blank constructor) and implement the interface. Its as simple as that you class can now be parsed
For Example:
public TestObject : IParse<TestObject>
{
public string Name { get; set; }
public TestObject Parse(object i)
{
if(i is string)
return new TestObject() { Name = i };
return (TestObject)i;
}
public TestObject GetDefault()
{
return new TestObject();
}
}
build the class and inhrit from IParse<>(Must have a blank constructor)
public void TestMethod(string obj = "blank")
{
var testObject = obj.ParseTo<TestObject>();
Now you can parse to your test object
Default return: 0
Default return: null
Default return: ""
Default return: 0
Default return: 0
Default return: 0
Default return: 0
Default return: 00000000-0000-0000-0000-000000000000
Default return: false
Default return: null
Default return: 1/1/0001 12:00:00 AM (Equals Date.MinValue)
Default return: null
Come conribute would love to hear you ideas Contribute
Got an issue here are my guidelines for reporting it.
This work is licensed under a MIT License
This repository contains the Open Source Code of Conduct