部分类问题 [英] Problem with Partial Class

查看:85
本文介绍了部分类问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我对部分课程有一个小问题



假设一个A类已在某些命名空间n中定义如下 -



Hi Everyone,

I've a small problem with partial class

Suppose that a class A is already defined as follow in some namespace n as -

namespace n
{
  public class A
  {
    private int i;
    ///Other methods and variables goes here
  }
}







现在,我需要向A类添加更多方法并访问变量我。



所以,我做了类似的事情 -






Now, I need to add more methods to the class A and also to access the variable i.

so, i did something like this -

namespace n
{
  public partial class A
  {
     public void SomeMethod()
     {
        this.i = i++;
     }
     ///Other methods and variables goes here
  }
}







但它在ASP.NET 4.0中不起作用。 />


任何人都可以提供解决方案,

提前使用Thanx:

-Satya182



[修改:代码标签用于在段落中插入片段,如果我想说使用 bool 变量。预标记用于更大的代码块]来自MSDN的




But it is not working in ASP.NET 4.0.

Can anyone has solution,
Thanx in advance :)
-Satya182

[Modified: code tags are meant for inserting snippets within paragraphs like if I wanted to say to use a bool variable. Pre tags are meant for bigger chunks of code]

推荐答案

:所有部分必须使用partial关键字(公共类定义(C#编程指南) [ ^ ])



第一个类必须有partial关键字,否则编译器在关闭它之前不会知道它应该查找类的其他部分。



如果你没有能力改变第一部分,那么你需要从第一个类继承一个新的类。然后你可以添加你想要的东西,但仍然可以访问原始部分。
from MSDN: "All the parts must use the partial keyword" (Partial Class Definitions (C# Programming Guide)[^])

The first class has to have the partial keyword or the compiler won't know that it should look for other parts of the class before closing it.

If you don't have the ability to change the first part, then you need to have a new class inherit from the first class. Then you can add as much as you want and still have access to the original parts.


命名空间n

{

public partial A级

{

private int i;

///其他方法和变量在这里

}

}
namespace n
{
public partial class A
{
private int i;
///Other methods and variables goes here
}
}


这篇关于部分类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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