I see no issues other than the compiler thinking that I should not do this. C# can't inherit from more than one base class, real bummer when you are used to be able to do this, frontally you can do several interfaces, so I abuse that to sneak in the features I need ;-)
You should check for null etc however here is a simplified version that implements Parse to get a class from a web service or database
/// <summary>
/// Implements parse
/// </summary>
/// <typeparam name="T">the type to parse</typeparam>
public interface IParse<T>
{
/// <summary>
/// implements parse from string to type
/// </summary>
/// <param name="text">value to parse</param>
/// <returns></returns>
static T Parse(string text)=>JsonConvert.DeserializeObject<T>(text, settings:new JsonSerializerSettings() { ConstructorHandling= ConstructorHandling.AllowNonPublicDefaultConstructor });
/// <summary>
/// implements parse from string to type
/// </summary>
/// <param name="text">value to parse</param>
/// <param name="settings">the settings to us</param>
/// <returns></returns>
static T Parse(string text, JsonSerializerSettings settings) =>JsonConvert.DeserializeObject<T>(text, settings);
}
Then I just call the interface
in code that has List as the return value.
Here is a snip where I read JSON from the database as populating it to a type that has Json implemented
//some plugging code
using (var reader = cmd.ExecuteReader(behavior: CommandBehavior.CloseConnection | CommandBehavior.SingleResult))
{
if (reader.HasRows)
{
while (reader.Read())
{
rows++;
try
{
var json = reader.GetString(0);
result.Add(IParse<T>.Parse(json));
}
catch
{
failed++;
}
}
}
}
//other plugging code
With >ver version 8 you have the default implementation so "pandora's box is open"