need help with code with reference and generic function
-
anyone know how to use ref to make this work? i keep getting error on load(i.Item2); i tried using ref but it didnt work.
public static List> DBlist;
DBlist.Add(new Tuple<object, string, Type>(A, "A", typeof(List)));
DBlist.Add(new Tuple<object, string, Type>(B, "B", typeof(List)));foreach (Tuple<Object, string, Type> i in DBlist)
{
Type temp = i.Item3;
i.Item1 = load(i.Item2);
}public static T load(string tablename)
-
anyone know how to use ref to make this work? i keep getting error on load(i.Item2); i tried using ref but it didnt work.
public static List> DBlist;
DBlist.Add(new Tuple<object, string, Type>(A, "A", typeof(List)));
DBlist.Add(new Tuple<object, string, Type>(B, "B", typeof(List)));foreach (Tuple<Object, string, Type> i in DBlist)
{
Type temp = i.Item3;
i.Item1 = load(i.Item2);
}public static T load(string tablename)
What error are you getting? "It didn't work" is hardly enough information for anyone to help you.
The difficult we do right away... ...the impossible takes slightly longer.
-
anyone know how to use ref to make this work? i keep getting error on load(i.Item2); i tried using ref but it didnt work.
public static List> DBlist;
DBlist.Add(new Tuple<object, string, Type>(A, "A", typeof(List)));
DBlist.Add(new Tuple<object, string, Type>(B, "B", typeof(List)));foreach (Tuple<Object, string, Type> i in DBlist)
{
Type temp = i.Item3;
i.Item1 = load(i.Item2);
}public static T load(string tablename)
-
anyone know how to use ref to make this work? i keep getting error on load(i.Item2); i tried using ref but it didnt work.
public static List> DBlist;
DBlist.Add(new Tuple<object, string, Type>(A, "A", typeof(List)));
DBlist.Add(new Tuple<object, string, Type>(B, "B", typeof(List)));foreach (Tuple<Object, string, Type> i in DBlist)
{
Type temp = i.Item3;
i.Item1 = load(i.Item2);
}public static T load(string tablename)
So you want to call a generic method with a dynamic type parameter? You'll need to use reflection to do that. Try something like this:
// Get the MethodInfo for the generic method:
MethodInfo baseMethod = typeof(YourClass).GetMethod("load",
BindingFlags.Public | BindingFlags.Static,
/* binder = */ null,
/* types = */ new[] { typeof(string) },
/* modifiers = */ null);foreach (Tuple<object, string, Type> i in DBlist)
{
// Substitute the type parameter in the generic method:
MethodInfo realMethod = baseMethod.MakeGenericMethod(i.Item3);// Call the static method, passing i.Item2 as the single parameter: i.Item1 = realMethod.Invoke(null, new\[\] { i.Item2 });
}
Where
YourClass
is the name of the class which contains theload<T>
method.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
anyone know how to use ref to make this work? i keep getting error on load(i.Item2); i tried using ref but it didnt work.
public static List> DBlist;
DBlist.Add(new Tuple<object, string, Type>(A, "A", typeof(List)));
DBlist.Add(new Tuple<object, string, Type>(B, "B", typeof(List)));foreach (Tuple<Object, string, Type> i in DBlist)
{
Type temp = i.Item3;
i.Item1 = load(i.Item2);
}public static T load(string tablename)
You have several problems, as Richard Deeming noted you can't use the load that way, you'll need to use Reflection. Also, it looks like you intend to assign the result of the load into the variable referenced by the .Item1 object of the Tuple. What your code actually does is try to assign into the .Item1 property of the Tuple. Tuples, once created, cannot be changed. The .Item1 property is get-only. You might be better off changing the DBList to hold delegates that actually do what you seem to want to happen. Something like:
public static List<Action> DBList;
DBList.Add(() => A = load<("A"));
DBList.Add(() => B = load<Boat>("B"));foreach (var loader in DBList)
{
loader();
}public static List<T> load<T>(string tablename)