The dynamic supports Interface #8915
Unanswered
Flithor
asked this question in
Language Ideas
Replies: 2 comments
-
I hate to play the broken record here, but this could be achieved with attributes and an analyzer: [DynamicInterface]
public interface MyDynamicInterface
{
public int IntegerNumber { get; set; }
}
public void TestMethod(
[DynamicArgument(typeof(MyDynamicInterface))] dynamic arg,
[DynamicArgument(typeof(MyDynamicInterface))] dynamic a,
[DynamicArgument(typeof(MyDynamicInterface))] dymamic b)
{
Console.WriteLine(arg.IntegerNumber);
Console.WriteLine(a.IntegerNumber);
Console.WriteLine(b.IntegerNumber);
} The analyzer would give an error if anyone actually implements the interface, and would give warnings/errors about accessing methods/properties that are not on the expected type. It wouldn't be as good as true IntelliSense, but it would work. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The idea is actually a kind of syntactic sugar:
As we all know, the
dynamic
keyword just skip the compilation check and compiles at runtime.But dynamic cannot enjoy the convenience of Intellisense.
This proposal could bring some benefits.
Example:
Because it is syntactic sugar, it actually compiles to:
MyDynamicInterface
will not compile, sotypeof(MyDynamicInterface)
is invalid.This proposal allows dynamic types to be treated as "strongly typed" with concrete definitions at compile time, and use same code on the classes that have no inheritance relationship but have the same members.
Beta Was this translation helpful? Give feedback.
All reactions