可能重复:
.NET: Determine the type of “this” class in its static method在静态方法中调用非静态GetType()?
你好,有没有办法来调用非静态GetType()
在非静态类,而无需使用typeof()
?
下面是我正在处理的代码示例。
private static ISession GetOrCreate(ISessionFactory factory)
{
if (HttpContext.Current!=null)
{
ISession session = GetExistingWebSession();
if (session == null)
{
session = OpenSessionAndAddToContext(factory);
}
else if (!session.IsOpen)
{
session = OpenSessionAndAddToContext(factory);
} return session;
}
}
private ISession GetExistingWebSession()
{
return HttpContext.Current.Items[GetType().FullName] as ISession;
}
您正在尝试按其类型查找对象,而不知道它的类型。这应该如何工作? – hvd 2012-04-12 09:19:41
为什么你不想使用'typeof'? – Jodrell 2012-04-12 09:21:04
为什么首先使用类型名称作为您的密钥? – Jodrell 2012-04-12 09:23:10