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.
Typevariable as a generic type argument, and your problem would be fixed by the solutions in the answers; specifically, stackoverflow.com/a/2604844/79439tag.id, because it doesn't know, not really, what the type oftagis. The other pitfall is that thetinMethodInfo method = t.GetMethod("get_json_data");needs to be whatever type thatget_json_data<t>is declared in. You should probably read up on both reflection and generics. It's a very broad topic, but for what you're trying to do, a clear understanding of both topics is paramount.