Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 泛型基类型的全名_C#_Generics_Reflection - Fatal编程技术网

C# 泛型基类型的全名

C# 泛型基类型的全名,c#,generics,reflection,C#,Generics,Reflection,我有一个类的层次结构,我试图从中获取类型名: class Alice :ThirdPersonCharacter<Foo, Bar> class ThirdPersonCharacter<A, B> :BaseHumanoidCharacter<A, B>, ISomeInterface where A : Something class BaseHumanoidCharacter<A, B> : Entity,

我有一个类的层次结构,我试图从中获取类型名:

class Alice
    :ThirdPersonCharacter<Foo, Bar>

class ThirdPersonCharacter<A, B>
    :BaseHumanoidCharacter<A, B>, ISomeInterface
    where A : Something

class BaseHumanoidCharacter<A, B>
    : Entity,
    ISomeOtherInterface
    where A : Something
当然,我真正想要的是:

{Name = "BaseHumanoidCharacter`2[[X.Y.Z.Foo, AssemblyName, version=123, Culture=whatever, PublicKey=stuff],[X.Y.Z.Bar, AssemblyName, version=123, Culture=whatever, PublicKey=stuff]]", FullName = "Something that isn't null"}
是否有办法修改此系统,以提供有用的类型,并填充它们的泛型参数,并且它们的全名不为null?

返回与您所寻找的非常相似的内容:

typeof(Tuple<int, string>).FullName
// System.Tuple`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

哎呀,格式上的差异只是我的一个错误。我将在问题中纠正这一点。真正的问题是FullName没有返回这样的字符串(这是我所期望的),因为某种原因,在本例中它返回null。我想这是关于整个系统的设置方式的一些具体问题,这就是为什么我给出了整个类层次结构的示例。更具体地说,我怀疑使用BaseType是错误的,但我不确定使用什么替代方法。@Martin
BaseType
可以。我无法重现您的问题,它显示为
null
。看看魔兽世界的输出,这是一个很棒的资源!我将编辑这个例子,看看我是否能正确地重现这个问题。好吧,我现在确实觉得自己像个白痴。系统实际上会扫描某些类型,并为它们执行此基本类型名称查找。我没有从系统中排除所有的基类型/接口/抽象类等。所以我猜在某种程度上,它是在看第三类personcharacter,然后被它噎住了。为了完全理解你的类层次结构,如果你也展示Alice派生的角色类,那会很有帮助-或者是一个拼写错误?是的,很抱歉。我指的是更具体的第三个人。
{Name = "BaseHumanoidCharacter`2[[X.Y.Z.Foo, AssemblyName, version=123, Culture=whatever, PublicKey=stuff],[X.Y.Z.Bar, AssemblyName, version=123, Culture=whatever, PublicKey=stuff]]", FullName = "Something that isn't null"}
typeof(Tuple<int, string>).FullName
// System.Tuple`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
var aliceTypes = BaseTypes(typeof(Alice)).Where(x => x == 
                           typeof(BaseHumanoidCharacter<Foo, Bar>))
                             .Select(a => new { a.Name, a.FullName }).ToArray();
Console.WriteLine(aliceTypes.Single());