I’m not sure how to correct this. I have
public void get_json(String TYPE)
{
Type t = Type.GetType("campusMap." + TYPE);
t[] all_tag = ActiveRecordBase<t>.FindAll();
}
But I always just get
Error 9 The type or namespace name ‘t’ could not be found (are you
missing a using directive or an assembly
reference?) C:_SVN_\campusMap\campusMap\Controllers\publicController.cs 109 17 campusMap
any ideas on why if I’m defining the type I am wishing to gain access to is saying it’s not working? I have tried using reflection to do this with no luck. Anyone able to provide a solution example?
[EDIT] possible solution
This is trying to use the reflection and so I’d pass the string and invoke the mothod with the generic.
public void get_json(String TYPE)
{
CancelView();
CancelLayout();
Type t = Type.GetType(TYPE);
MethodInfo method = t.GetMethod("get_json_data");
MethodInfo generic = method.MakeGenericMethod(t);
generic.Invoke(this, null);
}
public void get_json_data<t>()
{
t[] all_tag = ActiveRecordBase<t>.FindAll();
List<JsonAutoComplete> tag_list = new List<JsonAutoComplete>();
foreach (t tag in all_tag)
{
JsonAutoComplete obj = new JsonAutoComplete();
obj.id = tag.id;
obj.label = tag.name;
obj.value = tag.name;
tag_list.Add(obj);
}
RenderText(JsonConvert.SerializeObject(tag_list));
}
and the error I get is in..
obj.id = tag.id;
of
‘t’ does not contain a definition for ‘id’
and same for the two name ones.
You can’t pass a variable in as a generic parameter:
It’s complaining about the
<t>part. You can’t do that.I suggest you check this out: How do I use reflection to call a generic method?
Outside of that, I’d probably do everything you want to do with the generic type in a generic method, and then use reflection to call that generic function with the runtime type variable.
And then use the reflection tricks in the linked SO answer to call the
get_jsonfunction with the generic parameter.