如何在C#中的一个类的多个实例之间共享一个值
本文关键字:一个 共享 实例 之间 | 更新日期: 2023-09-27 18:26:55
我正在编写一个类库,用于我们公司的一些工程软件。该库用于定义结构型钢的特性。在我的每个类对象中,我都需要转到指定的文件夹,查找一些xml数据。
我如何创建一个公共变量,我可以在库类之外设置,并在实例之间共享
class Program
{
static void Main(string[] args)
{
string someCommonVarialble = @"c:'some'path'where'the'xmlData'are'stored";
// create some steel shapes
SteelBeamShape myBeam1 = new SteelBeamShape("W6x9");
SteelBeamShape myBeam2 = new SteelBeamShape("W10x22");
SteelPipeShape myPipe1 = new SteelPipeShape("10odx.375wall");
SteelPipeShape myPipe2 = new SteelPipeShape("24odx.750wall");
// do some work with objects here
}
}
public class SteelBeamShape
{
// constructor
public SteelBeamShape(string SteelBeamNominalValue)
{
// look up some properties base on nominal value in XML tables
this.xmlDataPath = someCommonVariable;
// do stuff ....
}
}
public class SteelPipeShape
{
// constructor
public SteelPipeShape(string SteelPipeNominalValue)
{
// look up some properties base on nominal value in XML tables
this.xmlDataPath = someCommonVariable;
// do stuff ....
}
}
}
我会考虑先加载xml数据,然后通过构造函数或将数据发送到类中执行实际工作的方法。
否则,类中可能有重复的代码在做基本相同的事情(加载数据、查找xml数据等)。
要在不同的类之间共享一个公共值,需要从一个公共祖先派生这些类。在您的情况下,您可以定义
public class SteelShape
{
public static string commonPath {get;set;}
... add other common functionality ....
... for example, how to load the xml file ....
public static XDocument LoadData(string fileName)
{
......
}
}
然后你的类从这个基类派生
public class SteelPipeShape : SteelShape
{
....
}
每当您需要引用类实例共享的公共变量时,您都可以使用祖先类名来引用它
SteelShape.commonPath;
所以综合起来你就有了
void Main()
{
// Set the path once and for all
SteelShape.commonPath = @"d:'temp";
SteelBeamShape myBeam1 = new SteelBeamShape("W6x9");
SteelBeamShape myBeam2 = new SteelBeamShape("W10x22");
SteelPipeShape myPipe1 = new SteelPipeShape("10odx.375wall");
SteelPipeShape myPipe2 = new SteelPipeShape("24odx.750wall");
}
public abstract class SteelShape
{
public static string commonPath {get;set;}
public static XDocument LoadData(string fileName)
{
......
}
}
public class SteelPipeShape : SteelShape
{
public SteelBeamShape(string SteelBeamNominalValue)
{
this.xmlDataPath = SteelShape.commonPath;
XDocument doc = SteelShape.LoadData("steelbeamshape.xml");
// do stuff ....
}
}
public class SteelBeamShape : SteelShape
{
public SteelPipeShape(string SteelPipeNominalValue)
{
// look up some properties base on nominal value in XML tables
this.xmlDataPath = SteelShape.commonPath;
XDocument doc = SteelShape.LoadData("steelpipeshape.xml");
// do stuff ....
}
}
您可以通过从公共基类继承所有Shape类并在基类中将公共变量定义为protected static
或public static
来实现这一点。
public abstract class BaseShape
{
public static string mSomeVar = "SomeValue";
}
public class SteelBeamShape : BaseShape
{
// constructor
public SteelBeamShape(string SteelBeamNominalValue)
{
// look up some properties base on nominal value in XML tables
this.xmlDataPath = this.mSomeVar;
// do stuff ....
}
}
public class SteelPipeShape : BaseShape
{
// constructor
public SteelPipeShape(string SteelPipeNominalValue)
{
// look up some properties base on nominal value in XML tables
this.xmlDataPath = this.mSomeVar;
// do stuff ....
}
}
public class Lib
{
private static string _commonVarialble;
public static string CommonVarialble
{
get { return _commonVarialble; }
set { _commonVarialble = value; }
}
public class SteelBeamShape
{
// constructor
public SteelBeamShape(string steelBeamNominalValue)
{
// look up some properties base on nominal value in XML tables
this.xmlDataPath = CommonVarialble;
// do stuff ....
}
}
}
现在您可以在库类之外设置CommonVarialble