i am trying to add generize list as class member in my UserRequest
class as follows
public class UserRequest<T> where T : RealmObject
{
public String userName { get; set; }
public String password { get; set; }
public String userId { get; set; }
public int page { get; set; }
public IList<T> requestList { get; set; }
}
now , whenever i want to send multiple Contact
or Customer
data i can simply use following request
in case of contact
UserRequest<Contact> userRequest = new UserRequest<Contact>();
userRequest.userId = "112";
userRequest.requestList = contactList;
this will result into following request data
{
"userId": "112",
"requsetList": [
{
"firstName": "john",
"lastName": "j",
"contactNumber": "(992) 414-9999",
}
]
}
in case of customer
i will use userRequest.requestList = customerList
, this approach helps me in avoid cerating saperate entiries for customer and contact
in UserRequest class
But the issue is when i just want to create simple UserRequest even then i have to pass someting in T.
for example if i want to use it for login request , following code won't work , so is there any way to use UserRequest
for generic request and non generic request
UserRequest userRequet = new UserRequest();
userRequest.userName ="abc";
userRequest.password="1234";
UserRequest
and UserRequest<T>
are two different types - you cannot create a UserRequest
if UserRequest<T>
is the only class you have written.
As you have found, UserRequest<T>
requires a generic type to be set. You could just create a UserRequest<object>
for logins, but this is a bit of a code smell and breaks the RealmObject
constraint. better is to make the generic type a sub-class of the non-generic one:
public class UserRequest
{
public String userName { get; set; }
public String password { get; set; }
public String userId { get; set; }
}
public class UserRequest<T> : UserRequest
where T : RealmObject
{
public int page { get; set; }
public IList<T> requestList { get; set; }
}