explicit-interface

Why would a class implement IDisposable explicitly instead of implicitly?

别来无恙 提交于 2020-01-09 03:49:06
问题 I was using the FtpWebResponse class and didn't see a Dispose method. It turns out that the class implements IDisposable, but does so explicitly so that you must first cast your instance to IDisposable before calling Dispose: // response is an instance of FtpWebResposne ((IDisposable) response).Dispose(); Why would the designer of a class such as this one choose to implement IDisposable explicitly? As Anthony Pegram says, doing things this way masks the fact that the object should be disposed

How can I call explicitly implemented interface method from PowerShell?

。_饼干妹妹 提交于 2019-12-29 07:39:07
问题 Code: add-type @" public interface IFoo { void Foo(); } public class Bar : IFoo { void IFoo.Foo() { } } "@ -Language Csharp $bar = New-Object Bar ($bar -as [IFoo]).Foo() # ERROR. Error: Method invocation failed because [Bar] doesn't contain a method named 'Foo'. 回答1: Bad news: It's a bug. https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=249840&SiteID=99 回答2: You can do something like $bar = New-Object Bar [IFoo].GetMethod("Foo").Invoke($bar, @()) You get (the reflection

How do I use an explicitly implemented interface property and wpf visiblity together properly?

走远了吗. 提交于 2019-12-24 08:49:43
问题 I have the following situation: I have a few ViewModel objects, some of which implement an interface ISomeInterface , some don't. The interfaces exposes a property called SomeEnumeration ( IEnumerable<T> ). For example: public sealed class ViewModelA : ViewModelBase, ISomeInterface { // ... IEnumerable<Foo> ISomeInterface.SomeEnumeration { get { ...; } } } public sealed class ViewModelB : ViewModelBase { // ... } My XAML, so far, has been designed in a way that both of the ViewModels happen

Every interface explicitly implemented? (IoC involved) [closed]

风格不统一 提交于 2019-12-24 04:41:09
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I'm very aware of the fact that such a question has probably been posted already. Yet with the involvement of IoC in this case and a lot of code I've seen a colleague in a company I'm new in made this question arise. Scenario: In the codebase of one product this colleague

Single fortran module in multiple files

杀马特。学长 韩版系。学妹 提交于 2019-12-10 18:55:24
问题 I just read the very good question/answers here about proper ways to use modules in Fortran. By writing subroutines in modules, one makes them explicit, in addition to clarifying the code. To my knowledge, a module has to be put in a single file, for instance "mod_exemple.f90". Programs I write are often very long, with many subroutines that indeed could be sorted by purpose and thus be put in modules. The problem: that would do very long module files, with hundreds of lines. Can you split a

how List<T> does not implement Add(object value)?

岁酱吖の 提交于 2019-12-10 14:42:37
问题 I believe it's pretty stupid, and I am a bit embarrassed to ask this kind of question, but I still could not find the answer: I am looking at the class List<T> , which implemetns IList . public class List<T> : IList one of the methods included in Ilist is int Add(object value) I understand that List<T> should not expose that method (type safety...), and it really does not. But how can it be? mustnt class implement the entire interface? 回答1: I believe that this (interface) method is

Using Explicit Interfaces to prevent accidental modification of properties in C#

倖福魔咒の 提交于 2019-12-10 10:56:10
问题 I stumbled on a feature of C# method resolution that I didn't notice before. Namely, when I explicitly implement an interface that supports a setter, and the implicit interface only offers a protected set, the compiler sensibly defers to the protected set when I call it. So I get most of the convenience of auto-implemented properties, but I can prevent accidental modification of fields by clients who shouldn't be changing them. As an example, virtual public DateTime CreatedOn { get; protected

Explicitly implemented interface and generic constraint

雨燕双飞 提交于 2019-12-09 17:17:42
问题 interface IBar { void Hidden(); } class Foo : IBar { public void Visible() { /*...*/ } void IBar.Hidden() { /*...*/ } } class Program { static T CallHidden1<T>(T foo) where T : Foo { foo.Visible(); ((IBar)foo).Hidden(); //Cast required return foo; } static T CallHidden2<T>(T foo) where T : Foo, IBar { foo.Visible(); foo.Hidden(); //OK return foo; } } Is there any difference (CallHidden1 vs. CallHidden2) is actual compiled code? Is there other differences between where T : Foo and where T :