Access global class name hidden by local name
Example:
namespace MyApp.NET
{
class Class1
{
public enum Types : byte = {t1, t2, t3};
public Type开发者_运维百科s m_type = t1;
}
class Class2
{
Class1 [] m_data = new Class1 [100];
public Class1 Class1 (int i) { return m_data [i]; }
void Method2 (Class1 c)
{
if (c.m_type == Class1.Types.t1) {}
}
}
class MyApp
{
}
}
Class1.Types.t1 isn't visible because of the method Class2.Class1. Is there a way to qualify Class1.Types.t1 so that it is accessible in the context outlined above? The issue is complicated by the namespace having the same partial name as another class.
Yes, by namespace: myNameSpace.Class1.Types.t1
. If these types were not in a namespace, then you could qualify it by forcing global scope: global::Class1.Types.t1
.
myNameSpace.Class1.Types.t1
if (c.m_type == myNameSpace.Class1.Types.t1)
global::MyApp.Net.Class1.Types.t1
You can just start qualifying as below:
if (c.m_type == NET.Class1.Types.t1) {}
Alternatively, if you add an alias for the Class1
class:
namespace MyApp.NET
{
using C1 = Class1;
...
}
your if
statement can then become:
if (c.m_type == C1.Types.t1) {}
This will let you do what you want.
精彩评论