[英]Accessing base class variables C#
I have base class我有基类
public class X
{
public int Id;
public int i;
}
and derived class和派生类
public class Y : X
{
//other variables
}
I have 3 projects in my solution.我的解决方案中有 3 个项目。 Class X is in project A, class Y in project B, and in project CI am trying to do: X 类在项目 A 中,Y 类在项目 B 中,在项目 CI 中我正在尝试执行以下操作:
Y newY = new Y();
newY. // this is where no variables from base class are showed (no Id or i)
If I reference project A from project C, it works.如果我从项目 C 中引用项目 A,它就可以工作。 But can I do it without referencing project A from C?但是我可以不从 C 引用项目 A 吗?
Existing references are : B -> A and C -> B现有的参考是:B -> A 和 C -> B
Project B
does required reference Project A
as class Y is derived from class X and some of the parent functionalities are written in Project A
only. Project B
确实需要引用Project A
因为类 Y 派生自类 X,并且一些父功能仅在Project A
中编写。
so adding reference of only ProjectB
(not ProjectA
) in your mail project, and trying to call functionalities of ProjectB
will surely fail in compile time.因此,在您的邮件项目中仅添加对ProjectB
(而不是ProjectA
)的引用,并尝试调用ProjectB
功能肯定会在编译时失败。
like below.像下面。
//of Project A
public class X
{
public string XStr;
public void MethodOfX()
{
}
}
and和
// of Prject B
public class Y : X
{
public string YStr;
public void MethodOfY()
{
//values form Project A is available
// as assembly reference has added here
YStr = XStr;
MethodOfX();
}
}
you can see, here you can access properties of class x too.你可以看到,在这里你也可以访问类 x 的属性。 as reference of ProjectA
has been added in ProjectB
.作为ProjectA
参考已添加到ProjectB
。
and in main class.并在主班。 by adding reference of ProjectB
only, if we try following.如果我们尝试以下操作,则仅添加对ProjectB
引用。
static void Main(string[] args)
{
Y y = new Y();
y.MethodOfY();
}
on Y y = new Y()
we will get this error on compile,在Y y = new Y()
我们将在编译时收到此错误,
The type 'ProjectA.X' is defined in an assembly that is not referenced.类型“ProjectA.X”是在未引用的程序集中定义的。 You must add a reference to assembly 'ProjectA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.您必须添加对程序集“ProjectA,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”的引用。
Here error is self explanatory.这里的错误是不言自明的。 it simply state that for doing Y y = new Y()
you will required to deal with ProjectA.X
(here in our case it is because we are inheriting it) and assembly which is having definition of ProjectA.X
is not yet referenced in main project.它只是说明为了执行Y y = new Y()
你需要处理ProjectA.X
(在我们的例子中是因为我们继承了它)和具有ProjectA.X
定义的程序集尚未在主要项目。
have a look in this question看看这个问题
As here we are having Y
inheriting X
we need to add reference of ProjectA
too in our main project because in this case Y
is holding some functionalities which is defined in ProjectA
.因为在这里我们让Y
继承X
我们需要在我们的主项目中添加对ProjectA
引用,因为在这种情况下Y
持有一些在ProjectA
定义的功能。
And as we haven't added ProjectA
's reference in our main project where we are trying to create object of Y, that object will not able to expose Y's those functionalities and members which are defined in ProjectA
由于我们没有在我们尝试创建 Y 对象的主项目中添加ProjectA
的引用,该对象将无法公开 Y 的那些在ProjectA
中定义的功能和成员
but if we change our code like below.但是如果我们像下面这样改变我们的代码。
//of Project A
public class X
{
public string XStr;
public void MethodOfX()
{
Console.WriteLine("Method of x");
}
}
and和
// of Prject B
public class Y
{
public string YStr;
public void MethodOfY()
{
Console.WriteLine("Method of Y");
X x = new X();
//values form Project A is available
// as assembly reference has added here
YStr = x.XStr;
x.MethodOfX();
}
}
Note that now Y is not inheriting X, but simply creates an instance of X and using it.请注意,现在 Y 没有继承 X,而是简单地创建 X 的一个实例并使用它。
static void Main(string[] args)
{
try
{
Y y = new Y();
y.MethodOfY();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.Read();
}
in our main project even after adding reference of only ProjectB
and not ProjectA
, above code will not cause any exception.在我们的主项目中,即使只添加了ProjectB
而不是ProjectA
引用,上面的代码也不会导致任何异常。 because here while creating object of Y
main project doesn't need to deal with ProjectA
as all functionlities and members of Y are in projectB
only.因为这里在创建Y
主项目的对象时不需要处理ProjectA
因为 Y 的所有功能和成员仅在projectB
。
And while calling the method MethodOfY()
even if that method internally calls method of x too, this calls will be handled if dll of ProjectA
is present in Application's run path (if you are debugging, usually in Debug folder).并且在调用MethodOfY()
方法时,即使该方法MethodOfY()
内部调用 x 的方法,如果ProjectA
dll 存在于应用程序的运行路径中(如果您正在调试,通常在 Debug 文件夹中),则将处理此调用。
Note: if dll of ProjectA is not there by any circumstances it will throw an exeption while calling MethodOfY()
saying that it cannot load ProjectA
dll (of course becuase it was not present there)注意:如果 ProjectA 的 dll 在任何情况下都不存在,它会在调用MethodOfY()
时抛出一个MethodOfY()
,说它无法加载ProjectA
dll(当然因为它不存在那里)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.