Unable to convert type System.Collection.Generic.List <T>

I am trying to use generics to reduce my code base, but have come across this situation. It seems I cannot successfully create a Google query that expresses what I'm trying to do.

In essence, I pass Generic to create List<T>, and then pass List<T>to a function that requiresList<SpecificClass>

My JSONUser Class

    public class JSONUser
    {
        public string DocumentId { get; set; }
        [JsonProperty(PropertyName = "UserName", Required = Required.Always)]
        public string UserName { get; set; }
        [JsonProperty(PropertyName = "FirstName", Required = Required.AllowNull)]
        public string FirstName { get; set; }
        [JsonProperty(PropertyName = "LastName", Required = Required.AllowNull)]
        public string Lastname { get; set; }
    }

Suppose there is a JSONCompany class with companies such as fields.

Main code:

static void CollectUserData()
{
     boolean j = GetJSON<JSONUser>();
     boolean k = GetJSON<JSONCompany>();
     ...
}

static boolean GetJSON<T>()
{
   ...
   // Get the JSON in a List
   List<T> oJSON = CallRest<T>();

   // Now depending on the type passed in, call a different
   // Process Function which expects a List<JSONUser> or
   // List<JSONCompany> parameter

   if (typeof(T) == typeof(JSONUser))
   {
       boolean result = ProcessUser(oJSON);
       ...
   }
   else if (typeof(T) == typeof(JSONCompany))
   {
       boolean result = ProcessCompany(oJSON);
       ...
   }
...
}

public boolean ProcessUser(List<JSONUser> JSONList)
{
    ...
}

public boolean ProcessCompany(List<JSONCompany> JSONList)
{
    ...
}

All is well until I call ProcessUser(oJSON);

It says there is no way to take Generic. When I try to do it, he says

Cannot hide type System.Collection.Generic.List<T>untilSystem.Collection.Generic.List<JSONUser>

Hope this is clear.

+4
source share
3 answers

ProcessUser et al IEnumerable<T>, :

public boolean ProcessUser(IEnumerable<JSONUser> JSONList)
{
    ...
}

public boolean ProcessCompany(IEnumerable<JSONCompany> JSONList)
{
    ...
}

:

boolean result = ProcessUser(oJSON.Cast<JSONUser>());

:

boolean result = ProcessUser(oJSON.Cast<JSONUser>().ToList());

, , . (///..)

+1

OP , , , . OP, , ProcessUser/ProcessCompany / , SQL. JSONUser JSONCompany ( ), , , SQL- .

, , , , typeof (T).

public interface IInsertToSql
{
    void InsertToSql(SqlCommand cmd);
}

public class JSONUser : IInsertToSql
{
    public string UserID { get; set; }
    public void InsertToSql(SqlCommand cmd)
    {
        cmd.CommandText = "sp_insertJsonUser";
        cmd.Parameters.AddWithValue("@jsonUserId", UserID);
        // More parameters
        cmd.ExecuteNonQuery();
    }
}

public class JSONCompany : IInsertToSql
{
    public string CompanyID { get; set; }
    public void InsertToSql(SqlCommand cmd)
    {
        cmd.CommandText = "sp_insertJsonCompany";
        cmd.Parameters.AddWithValue("@jsonCompanyId", CompanyID);
        // More parameters....
        cmd.ExecuteNonQuery();
    }
}

void Main()
{
    List<JSONUser> users = GetJSON<JSONUser>();
    List<JSONCompany> companies = GetJSON<JSONCompany>();
    BulkUpload(users);
    BulkUpload(companies);
}

static void BulkUpload<T>(List<T> list)
    where T: IInsertToSql
{
    using(SqlConnection conn = new SqlConnection(""))
    {
        conn.Open();
        foreach(T t in list)
        {
            using (SqlCommand cmd = conn.CreateCommand()) t.InsertToSql(cmd);
        }
    }
}

static List<T> GetJSON<T>()
{
    // OP code to get the list of items, instead of...
    return new List<T>();
}
+1

, , . typeof (T), ( ), , , .

, , ... , :

ProcessUser ProcessCompany ( , ), , JSONUser, JSONCompany , , ICanProcess, Process(), bool. :

interface ICanProcess
{
    bool Process();
}
class JSONUser : ICanProcess
{
    // implement Process for this object
}
static bool GetJSON<T>()
    where T: ICanProcess
{
    ...
    bool result = true;
    foreach(T it in oJSON)
    {
        // I assume the result should be AND of all items process result
        result = result && it.Process();  
    }
}
0

All Articles