简体   繁体   中英

use Type.GetType(string) in C# 2.0 .net but type or namespace name 't' could not be found

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:

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);

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM