I have a class called SystemUser
as defined below:
public class SystemUser
{
public int userID;
public string userName;
public string email;
public SystemUser(int userId, string userName, string email)
{
this.userID = userId;
this.userName = userName;
this.email = email;
}
public static explicit operator System.String(SystemUser su)
{
return su.userName;
}
public static implicit operator SystemUser(System.String s)
{
SystemUser thing = new SystemUser(-1, s);
return thing;
}
}
From what I'm able to determine, this should allow me to perform the following, where user
is a variable of type object
:
List<SystemUser> systemUsers = new List<SystemUser>();
systemUsers.Add((SystemUser)user); // causes exception
However, if user
is a string, I always get the InvalidCastException: "Unable to cast object of type 'System.String' to type 'SystemUser'"
What am I missing here?