M
Milan
Hi experts,
Can someone help me to understand why inside function
"generic_func"
object of type "TS" is not reckognized as object of type "string"
but it is recognized as object of type "object" ?
later because of wrong type I get exception:
"Objekt of Type \"System.Collections.Generic.List`1[System.String]\" can not
be converted in Type \"System.Collections.Generic.List`1[System.Object]\".
How I can make that List<some_generic_type> reckognize type of TS correctly?
BR,
Milan.
class Matematician
{
public List<string> getSomeStringType()
{
List<string> lst_string = new List<string>();
return lst_string;
}
}
public void generic_func<TS, TM>(TS obj_string, TM obj_math)
{
Type tt = obj_string.GetType();
//correctly reckognized string type !{Name = "String" FullName =
"System.String"}
try
{
List<TS> obj_generic =
(List<TS>)obj_math.GetType().InvokeMember("getSomeStringType",
BindingFlags.Default | BindingFlags.InvokeMethod, null, obj_math, null);
}
catch (Exception ex)
{
//"Objekt of Type
\"System.Collections.Generic.List`1[System.String]\" can not be converted in
Type \"System.Collections.Generic.List`1[System.Object]\".
Console.WriteLine("error");
}
}
static void Main(string[] args)
{
Class1 obj_main = new Class1(); //Class1 holds main method !
string str_test_1 = "KingOfTheKongo";
object obj_math = new Matematician();
object obj_str = str_test_1;
// In real case (more coplicated)
// input parameters: obj_str, obj_math
// are created using reflection function
// Activator.CreateInstance
// and they are of type "object"
obj_main.generic_func(obj_str, obj_math);
}
Can someone help me to understand why inside function
"generic_func"
object of type "TS" is not reckognized as object of type "string"
but it is recognized as object of type "object" ?
later because of wrong type I get exception:
"Objekt of Type \"System.Collections.Generic.List`1[System.String]\" can not
be converted in Type \"System.Collections.Generic.List`1[System.Object]\".
How I can make that List<some_generic_type> reckognize type of TS correctly?
BR,
Milan.
class Matematician
{
public List<string> getSomeStringType()
{
List<string> lst_string = new List<string>();
return lst_string;
}
}
public void generic_func<TS, TM>(TS obj_string, TM obj_math)
{
Type tt = obj_string.GetType();
//correctly reckognized string type !{Name = "String" FullName =
"System.String"}
try
{
List<TS> obj_generic =
(List<TS>)obj_math.GetType().InvokeMember("getSomeStringType",
BindingFlags.Default | BindingFlags.InvokeMethod, null, obj_math, null);
}
catch (Exception ex)
{
//"Objekt of Type
\"System.Collections.Generic.List`1[System.String]\" can not be converted in
Type \"System.Collections.Generic.List`1[System.Object]\".
Console.WriteLine("error");
}
}
static void Main(string[] args)
{
Class1 obj_main = new Class1(); //Class1 holds main method !
string str_test_1 = "KingOfTheKongo";
object obj_math = new Matematician();
object obj_str = str_test_1;
// In real case (more coplicated)
// input parameters: obj_str, obj_math
// are created using reflection function
// Activator.CreateInstance
// and they are of type "object"
obj_main.generic_func(obj_str, obj_math);
}