'Using a type name within a static method that matches a property with the same name causes problems. Parser error?
I have a class that contains a static factory method to inspect the type provided to the factory and create an instance. The enclosing class happens to have a property called Type and for some reason, it seems like it's treating the name Type as the instance property within this static method. I'm trying to utilize the static method on the Type class.
public class SomeTypeBad
{
public static void SomeStaticMethod(Type type)
{
var _ = Type.GetTypeCode(type); // ERROR: tries to use instance property?
}
public SomeEnum Type { get; } // property `Type` of type `SomeEnum`
}
public class SomeTypeGood
{
public static void SomeStaticMethod(Type type)
{
var _ = Type.GetTypeCode(type); // everything is suddenly fine?
}
public Type Type { get; } // property `Type` of type `System.Type`
}
In this context, it's clearly a static method so Type shouldn't resolve to the Type property. Is this a bug in the parser? Or am I doing something illegal here?
It's weird that it's only a problem because of the property type. It only seems to happen if the property Type had a type other than the System.Type type. If it was, that type, then it would work as expected.
(I know there's plenty of ways to resolve this, fully qualify the name, change the name, etc. but that's not the question here)
(also notice the reference count on the property)
To double check it wasn't some weird corner case problem with the System.Type class, tried on another class and same consistent behavior (but different error).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|



