1

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.

Quantum
  • 1,456
  • 3
  • 26
  • 54
  • 2
    possible duplicate of [Setting generic type at runtime](http://stackoverflow.com/questions/2604743/setting-generic-type-at-runtime) – thecoop Dec 20 '11 at 17:12
  • I don't see it as beuing the same.. but I could be wrong. – Quantum Dec 20 '11 at 18:02
  • The problem is the same - trying to use a `Type` variable as a generic type argument, and your problem would be fixed by the solutions in the answers; specifically, http://stackoverflow.com/a/2604844/79439 – thecoop Dec 20 '11 at 18:04
  • I don't know.. That answer may be clear to you but Digitlworld's seems more clear to me here .. :/ why I'm working this out, just not sure how to use that one you pointed out. – Quantum Dec 20 '11 at 18:06
  • The additional problem here is you're using a generic type in a way that the compiler would have to know ahead of time the members it contains. It can't find `tag.id`, because it doesn't know, not really, what the type of `tag` is. The other pitfall is that the `t` in `MethodInfo method = t.GetMethod("get_json_data");` needs to be whatever type that `get_json_data` 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. – digitlworld Dec 20 '11 at 19:57
  • possible duplicate of [C# Dynamic Generic Type](http://stackoverflow.com/questions/2078914/c-sharp-dynamic-generic-type) – nawfal Jan 17 '14 at 13:27
  • Ok.. there is no way that it can be both a duplicate http://stackoverflow.com/q/2604743/746758 and http://stackoverflow.com/q/2078914/746758 as they are not duplicates of each other.. just saying, all 3 have different resolutions to the nuanced of an issue that has many facets during implementation. – Quantum Jan 17 '14 at 15:38

3 Answers3

4

You can't pass a variable in as a generic parameter:

t[] all_tag = ActiveRecordBase<t>.FindAll();

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.

public void get_json<t>()
{
    t[] all_tag = ActiveRecordBase<t>.FindAll();
    //Other stuff that needs to use the t type.
}

And then use the reflection tricks in the linked SO answer to call the get_json function with the generic parameter.

MethodInfo method = typeof(Sample).GetMethod("get_json");
MethodInfo generic = method.MakeGenericMethod(typeVariable);
generic.Invoke(this, null);
Community
  • 1
  • 1
digitlworld
  • 1,046
  • 7
  • 13
  • I edited the question to reflect the possible suggestiong here. tk – Quantum Dec 20 '11 at 17:58
  • "You can't pass a variable in as a generic parameter" Is fully right... and What I ended up doing was setting up an interface and then made the the class types I needed to call from the parameter the and they inherite from that. – Quantum Jan 20 '12 at 02:48
4

Your program indicates a fundamental misunderstanding about how C# works. The type of all_tag and the type value of the type argument must both be known to the compiler before the program is compiled; you do not determine that type until the program is already running, which clearly is after it is compiled.

You can't mix static compile-time typing with dynamic runtime typing like this. Once you are doing something with Reflection, the whole thing has to use Reflection.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • No I get that it is supost to be "knowen" .. but that is not what the need is. the point is that I have to have a dynamic part here, or face 35+ copy pasting of the same 6 line method where I only change ... and that seems like C# has to have a solution too that is clean and clear. I know moving to 3.5 would give dynamic types but we can't do that. Open to ideas :D – Quantum Dec 20 '11 at 18:11
0

Can you delegate the choice of type to the calling code? You can't specify a runtime type as a generic, compile-time type, but if you could delegate the choice of type to the caller, you might be able to accomplish what you want.

public ??? get_json<T>() // seems like this should return something, not void
{
     var collection = ActiveRecord<T>.FindAll();
     // do something with the collection
}

Called as

get_json<CampusMap.Foo>();

Even if you didn't know the type at compile time, it might would be easier to call this way via reflection, see https://stackoverflow.com/a/232621.

Community
  • 1
  • 1
tvanfosson
  • 524,688
  • 99
  • 697
  • 795