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#_Asp.net - Fatal编程技术网

C# 如何比较我的实际母版页

C# 如何比较我的实际母版页,c#,asp.net,C#,Asp.net,我有2个在我的项目母版页 将母版页转换为普通页,将母版页转换为弹出页 我有一个由所有页面继承的类BasePage,在BasePage中,我需要验证哪个是实际使用的页面 例: if(Master.GetType()==typeof(Master) 如何测试它?检查母版页类型的最简单方法是使用is关键字: if (this.Master is MasterPageCommon) { } else if (this.Master is MasterPagePopup) { } 检查母版页类型的最

我有2个在我的项目母版页

母版页转换为普通页
,将
母版页转换为弹出页

我有一个由所有页面继承的类
BasePage
,在BasePage中,我需要验证哪个是实际使用的页面

例:

if(Master.GetType()==typeof(Master)


如何测试它?

检查母版页类型的最简单方法是使用
is
关键字:

if (this.Master is MasterPageCommon) {

} else if (this.Master is MasterPagePopup) {

}

检查母版页类型的最简单方法是使用
is
关键字:

if (this.Master is MasterPageCommon) {

} else if (this.Master is MasterPagePopup) {

}

你应该能做到

if(page.Master is PopUpMaster)
{
   //Do Something
}
else if (page.Master is NormalMaster)
{
   //Do Something
}

你应该能做到

if(page.Master is PopUpMaster)
{
   //Do Something
}
else if (page.Master is NormalMaster)
{
   //Do Something
}

is
运算符便于检查类型

如果这两个母版(我称之为MasterPage和MasterPagePopup)是从一个共同的祖先(Page?)继承的,而不是彼此继承的,您可以这样做:

if(Master is MasterPage) 
  { do some stuff; }
if(Master is MasterPagePopup)
  { do other stuff; }
唯一的问题是如果一个母版从另一个母版继承;如果MasterPagePopup从母版页继承,则上述两种情况对MasterPagePopup都适用,因为他既是母版页又是MasterPagePopup。但是,
if…else if
可以解决此问题:

if(Master is MasterPagePopup)
  { do other stuff; }
else if(Master is MasterPage) // popup is already handled and will not hit this
  {do some stuff; }

is
运算符便于检查类型

如果这两个母版(我称之为MasterPage和MasterPagePopup)是从一个共同的祖先(Page?)继承的,而不是彼此继承的,您可以这样做:

if(Master is MasterPage) 
  { do some stuff; }
if(Master is MasterPagePopup)
  { do other stuff; }
唯一的问题是如果一个母版从另一个母版继承;如果MasterPagePopup从母版页继承,则上述两种情况对MasterPagePopup都适用,因为他既是母版页又是MasterPagePopup。但是,
if…else if
可以解决此问题:

if(Master is MasterPagePopup)
  { do other stuff; }
else if(Master is MasterPage) // popup is already handled and will not hit this
  {do some stuff; }

不太合理,你能试着解释一下吗?我的项目中有两个母版页。母版页到普通页,母版页到弹出页。我有一个由所有页继承的类BasePage,在BasePage中,我需要验证实际使用的母版页。你能理解我吗?两个母版都有自定义类型吗页面?为什么不能使用
getType()==typeof()
您提到的方法?所以,当我测试它时,类型是不同的。当然,我必须将我的实际母版页与类的类型进行比较。这不太有意义,您能试着解释一下吗?我的项目中有两个母版页。母版页是普通页,母版页是弹出页。我有一个类的基本页由所有页面继承,在BasePage中,我需要验证实际使用的MaterPage是什么。你能理解我吗?两个母版页都有自定义类型吗?为什么不能使用
getType()==typeof()
您提到的方法?所以,当我测试它时,类型是不同的。当然,我必须将我的实际母版页与类的类型进行比较?