I am working with third party services, my requests are working fine, but I need to pass this result to my model so that I can separate it into an array and be able to set properties based on index as they don't have a key. I am passing this value through an abstract class and inheriting it in my model but when trying to call the property in my model constructor it always returns null even though in my abstract class it has been set.
I have already tried making my property abstract and overriding but it doesn't work. I also tried to pass the values through the constructors but being a T instance and it doesn't allow me, maybe I am doing something wrong.
The question is, in what way can I get the value set?
This is my HttpBase.cs
public class HttpBase
{
public TResponse Post<TResponse>(string url, Dictionary<string, string> body)
where TResponse : ClassA, new()
{
string result = Post(url, body).Result.ToString();
TResponse response = new TResponse
{
JSON = result // this is where I try to set my property
};
return response;
}
public async Task<string> Post(string url, Dictionary<string, string> body)
{
// returns a string separated by '|'. Ex: "abc|abcd|abcde"
}
}
ClassA.cs
public abstract class ClassA
{
public string JSON { get; set; }
}
MyModel.cs
public class MyModel : ClassA
{
public MyObject()
{
string json = this.JSON; // null
string[] arr = json.Split('|', '\n').ToArray(); // throws NullReferenceException
}
[Position(1)] //Custom Attribute
public string Name{ get; set; }
}