简体   繁体   English

在C#2.0 .net中使用Type.GetType(string),但找不到类型或名称空间名称't'

[英]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 错误9找不到类型或名称空间名称“ t”(您是否缺少using指令或程序集引用?)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' 的“ t”不包含“ 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. 它在抱怨<t>部分。 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. 然后使用链接的SO答案中的反射技巧来调用带有泛型参数的get_json函数。

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. 您的程序表明对C#的工作原理有基本的误解。 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; 在编译程序之前 ,编译器必须同时知道all_tag的类型和type参数的类型值。 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 . 即使您在编译时不知道类型,通过反射来调用这种方法也 可能 会更容易,请参见https://stackoverflow.com/a/232621

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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