public interface IType
{
public string Type { get; set; }
}
public class TypeA : IType
{
public string Type { get; set; }
public string FancyA { get; set; }
}
public class TypeB : IType
{
public string Type { get; set; }
public string FancyB { get; set; }
}
void Get(json)
{
var x = GetJson;
IType returnObject = null;
switch(x.GetType)
{
"TypeA":
returnObject = JsonSerializer.Deserialize<TypeA>(x);
break;
"TypeB":
returnObject = JsonSerializer.Deserialize<TypeB>(x);
break;
}
return returnObject;
}
void DoParse()
{
var callbacks = Dictionary<string, Action<IType>> { ("TypeA", DoStuffTypeA) };
while(true)
{
var content = Retrieve();
var parsed = Get(content);
var callback = callbacks[parsed.Type]
callback.Invoke(parsed)
}
}
// Works
void DoStuffTypeA(IType content)
{
//...
}
// Doesn't work
void DoStuffTypeA(TypeA content)
{
//...
}
public interface IType
{
public string Type { get; set; }
}
public class TypeA : IType
{
public string Type { get; set; }
public string FancyA { get; set; }
}
public class TypeB : IType
{
public string Type { get; set; }
public string FancyB { get; set; }
}
void Get(json)
{
var x = GetJson;
IType returnObject = null;
switch(x.GetType)
{
"TypeA":
returnObject = JsonSerializer.Deserialize<TypeA>(x);
break;
"TypeB":
returnObject = JsonSerializer.Deserialize<TypeB>(x);
break;
}
return returnObject;
}
void DoParse()
{
var callbacks = Dictionary<string, Action<IType>> { ("TypeA", DoStuffTypeA) };
while(true)
{
var content = Retrieve();
var parsed = Get(content);
var callback = callbacks[parsed.Type]
callback.Invoke(parsed)
}
}
// Works
void DoStuffTypeA(IType content)
{
//...
}
// Doesn't work
void DoStuffTypeA(TypeA content)
{
//...
}