如何获得基类名称? [英] How to get base class Name ?

查看:125
本文介绍了如何获得基类名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨代码项目......

i希望获得类型的类型

示例:我有类名A

ClassA:System .Windows.Forms.Form



然后我有classB哪个ClassB:ClassA - 继承

然后我有C级ClassC :ClassB

和D级ClassD:ClassB也是。



i使用这个C级来显示数据

frmViewData:ClassC



当我在mdiForm上显示这个frmViewData时...我想检查frmViewData是否是ClassC。



示例。



  void  toolStripView_click( object  sender,EventArgs e)
{
frmViewData f = new frmViewData;
f.MdiParent = ;

// 我试试这个但不能正常工作
if (f.GetType()== typeof (classC)
{
f。 SomeCustomProperties = // 一些自定义值;
}
f.Show() ;
}



此代码不起作用。当我使用消息框检查时

  void  toolStripView_click( object  sender,EventArgs e)
{
frmViewData f = new frmViewData;
f.MdiParent = this ;
MessageBox.Show(f.GetType( ).ToString());

}



结果是消息框中的WinnSystem.frmViewData。

这是检查班级的任何其他方式吗?



thx很多。

解决方案

something.GetType()== typeof(someOther)检查的类型 someOther 的类型相等。如果你想检查某事 someOther 继承然后使用关键字

  if (f   classC)
{
// ...
}





编辑:如果类型相同,也将返回true(所以不仅仅是实际派生的类型)。



编辑2:正如PIEBALDConsult在他的解决方案中指出的那样,有其他选择:Type.IsAssignableFrom(..)和Type.IsInstanceOfType(..)(与关键字完全相同)是。哪一个最方便使用取决于你必须与什么(一个对象的实例/一个类型对象/一个静态类型)进行比较。见这里:

http ://stackoverflow.com/questions/15853159/isassignablefrom-isinstanceoftype-and-the-is-keyword-what-is-the-difference [ ^ ]


我使用 Type.IsAssignableFrom https://msdn.microsoft.com/en-us/library/system.type.isassignablefrom(v = vs.110).aspx [ ^ ]



typeof(classC).IsAssignableFrom(f.GetType())


使用reflection - 输入superClass = frmViewDa ta.GetType()基本类型;

Hi code project...
i want to get the type of class
example: i have class name A
ClassA : System.Windows.Forms.Form

and then i have classB which ClassB : ClassA -- Inherits
and then i have class C which ClassC : ClassB
and class D which ClassD : ClassB too.

i use this class C for show data
frmViewData : ClassC

when i show this frmViewData on mdiForm.. i want to check if frmViewData is ClassC.

example.

void toolStripView_click (object sender, EventArgs e)
{
  frmViewData f = new frmViewData;
  f.MdiParent = this;

  //i try this but not work
  if ( f.GetType() == typeof(classC)
  {
    f.SomeCustomProperties = //Some custom value;
  }
  f.Show();
}


this code is not work. when i check using messagebox

void toolStripView_click (object sender, EventArgs e)
{
  frmViewData f = new frmViewData;
  f.MdiParent = this;
  MessageBox.Show(f.GetType().ToString());

}


the result is " WinnSystem.frmViewData " at messagebox.
is that any other way to check the class ??

thx a lot.

解决方案

something.GetType() == typeof(someOther) checks whether the type of something is equal to the type of someOther. If you want to check whether something inherits from someOther then use the keyword is:

if (f is classC)
{
  // ...
}



Edit: In case the types are equal, is will also return true (so not only for actually derived types).

Edit 2: As PIEBALDConsult has pointed out in his solution, there are alternatives: Type.IsAssignableFrom(..) and Type.IsInstanceOfType(..) that do (mostly) the same thing as the keyword is. Which one is most convenient to use depends on what you have to compare to what (an instance of an object / a type object / a static type). See here:
http://stackoverflow.com/questions/15853159/isassignablefrom-isinstanceoftype-and-the-is-keyword-what-is-the-difference[^]


I use Type.IsAssignableFrom : https://msdn.microsoft.com/en-us/library/system.type.isassignablefrom(v=vs.110).aspx[^]

typeof(classC).IsAssignableFrom(f.GetType())


Use reflection - Type superClass = frmViewData.GetType().BaseType;


这篇关于如何获得基类名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆