Proposal: Allow Anonymous Type Implement Interface(s) #2298
Replies: 10 comments
-
@Timothy-Liu what is the benefit? Wherever you are writing interface, you can simply declare it as a class. |
Beta Was this translation helpful? Give feedback.
-
There is still situation that you may want to combine two or more interfaces, but you can't inherit two classes! |
Beta Was this translation helpful? Give feedback.
-
Main benefit would be multi callback calls..
Imagine a function like DoThings(string someInput, IResultCallback callback) you could call it like this:
|
Beta Was this translation helpful? Give feedback.
-
I would like to see this as well. My use case right now is MassTransit where the recommendation is to use interface types as message types:
If you follow this, then you will find yourself often in a situation where you will have to do something like this: await publishEndpoint.Publish<IOrderSubmitted>(new
{
OrderId = "27",
OrderDate = DateTime.UtcNow,
}); The anonymous type you pass there is supposed to be compatible to So on the consumer side, you can totally work with interfaces only. Doing so on the producing side means that you will either have to create additional classes that don’t do anything other than implementing the interface, or you will have to deal with anonymous types. That has the drawback that you do not get any IntelliSense though which means that you will not get any validation of the properties. Having an anonymous type that implements an interface would help this a lot, e.g.: await publishEndpoint.Publish(new IOrderSubmitted
{
OrderId = "27",
OrderDate = DateTime.UtcNow,
}); |
Beta Was this translation helpful? Give feedback.
-
You may like to check out this Roslyn analyzer I wrote to structurally compare an anonymous type with a message contract + code fix for adding properties from the message contract that are missing in the anonymous type: https://github.com/RemcoBlok/MessageContractAnalyzer I also submitted a Pull Request to add this Roslyn analyzer to MassTransit, which was accepted. The Roslyn analyzer is now further developed by MassTransit and a NuGet package made available (still in prerelease at time of writing). See https://twitter.com/PhatBoyG/status/1235331842217332737 The use case extends beyond MassTransit as well. I use EventStore in a CQRS + Event Sourcing pattern and all events stored in EventStore are also defined as read-only interfaces that I initialize using anonymous types in combination with the Roslyn analyzer I wrote. |
Beta Was this translation helpful? Give feedback.
-
@Spacefish That callback example looks quite similar to some of the things discussed in #2517 |
Beta Was this translation helpful? Give feedback.
-
@GalaxiaGuy well there are straight forward ways to do things.. This is the most obvious intuitive one IMHO.. Though in don´t agree with the generic interface that is implemented by a funciton and not a class in #2517 as this is already covered by delegates.. |
Beta Was this translation helpful? Give feedback.
-
if that's the case, then why do we have anonymous classes? the same argument could be used to eliminate those... |
Beta Was this translation helpful? Give feedback.
-
I am somewhat new to c# due to work circumstances and was surprised to find this is not a language level feature. Especially on callback driven code there is quick convolution of short dispatch handlers that could be better handled as anonymous interface implementations on caller side. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions