全球对象 [英] global objects

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

问题描述

大家好!

我感觉有点迷失在这里。我如何制作全球物品给

a类。

我想要如果你在asp.net下运行一个组件,可以使一些对象像httpcontext对象一样可用



提前付款

ps

Hi ALL!
I am feeling kinda lost here.How do i make global objects available to
a class.
e.g i want to make some objects available like the httpcontext object
available if you run a component under asp.net.
Thanks in advance
ps

推荐答案

只需做出适当的参考....


-------

问候,

C#,VB.NET,SQL SERVER,UML,设计模式面试问题书
http://www.geocities.com/dotnetinterviews/

我的访谈博客
http://spaces.msn.com/members/dotnetinterviews/

Just make approriate reference....

-------
Regards ,
C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/
My Interview Blog
http://spaces.msn.com/members/dotnetinterviews/


谢谢,但这不是很有帮助。你能举个例子吗?


谢谢

Thanks but that was not really helpful. Can you please give an example?

Thanks


你问的问题是:


如何实现单例模式?

这里是如何:


//密封课程所以它不能继承

密封的公共课MySingleton

{


// MySingleton的实例将是全球的app

//注意它是静态的!!!

静态MySingleton _context;

//包含用于演示目的的数据的某个字段

string _data;

//隐藏构造函数

private void MySingleton {}

//返回全局的静态属性实例

static public void MySingleton Context

{

get

{

/ /锁定线程安全的类型

锁定(typeof(MySingleton)

{

//如果它从未被实例化它的实例

if(_context = null)_context = n ew MySingleton();

}

//返回全局实例

return _context;

}

}


//返回MySingleton管理数据的statndard属性

public void string数据

{

get

{

返回_data;

}

set

{

lock(this)

{

_data = value;

}

}

}

}


用法:


MySingleton.Context.Data =" foobar";

Console.Writeline(MySingleton.Context.Data);


读取此内容:
http://en.wikipedia.org/wiki/Singleton_pattern

购买:
http://en.wikipedia。 org / wiki / Design_Patterns

The question you are asking is:

"How do I implement singleton pattern?"
here is how:

//seal the class so it can''t be inherited
sealed public class MySingleton
{

// instance of MySingleton that will be global to the app
// note it is static!!!
static MySingleton _context;
// some field containing data for demo purposes
string _data;
// hide the constructor
private void MySingleton {}
// static property to return the global instance
static public void MySingleton Context
{
get
{
//lock the type for thread safety
lock( typeof(MySingleton)
{
//instance it if it has never been instanced
if (_context = null) _context = new MySingleton();
}
//return the global instance
return _context;
}
}

// statndard property to return data that MySingleton manages
public void string Data
{
get
{
return _data;
}
set
{
lock(this)
{
_data = value;
}
}
}
}

usage:

MySingleton.Context.Data = "foobar";
Console.Writeline(MySingleton.Context.Data);

read this:
http://en.wikipedia.org/wiki/Singleton_pattern
buy this:
http://en.wikipedia.org/wiki/Design_Patterns


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

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