码:
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.
Run Code Online (Sandbox Code Playgroud)
错误:
方法调用失败,因为[Bar]不包含名为'Foo'的方法.
我有一个非常简单的场景:" 人 "可以是公司的" 客户 "或" 员工 ".
A" 人 ",可以通过电话与"被称为呼叫 "的方法.
根据" 人 "在通话环境中扮演的角色,例如新产品的公告或组织变更的公告,我们应该使用为" 客户 "角色提供的电话号码或提供的电话号码为" 员工 "角色.
以下是对情况的总结:
interface IPerson
{
void Call();
}
interface ICustomer : IPerson
{
}
interface IEmployee : IPerson
{
}
class Both : ICustomer, IEmployee
{
void ICustomer.Call()
{
// Call to external phone number
}
void IEmployee.Call()
{
// Call to internal phone number
}
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码不能编译并产生错误:
error CS0539: 'ICustomer.Call' in explicit interface declaration is not a member of …Run Code Online (Sandbox Code Playgroud) 码:
public interface IFoo
{
void Bar();
}
public class FooClass : IFoo
{
/// <summary> ... </summary>
/// <seealso cref="?"/> //How do you reference the IFoo.Bar() method
public void Bar() { }
/// <summary> ... </summary>
/// <seealso cref="?"/> //How do you reference the standard Bar() method
void IFoo.Bar() { }
}
Run Code Online (Sandbox Code Playgroud)
我的猜测是:
<seealso cref="IFoo.Bar()"/> //Explicitly implemented interface method
<seealso cref="Bar()"/> //Standard method
Run Code Online (Sandbox Code Playgroud)
但是,我不确定.ECMA指南没有帮助区分,所以我想我正在寻找保证我的猜测是正确的.
我想实现的是同时实现了一个C++/CLI类IList和IList<T>.
由于它们具有重叠的名称,我必须明确地实现其中一个,并且自然选择应该是IList.
索引器的隐式实现是:
using namespace System::Collections::Generic;
generic<class InnerT> public ref class MyList : public System::Collections::IList, IList<InnerT> {
// ...
property InnerT default[int]{
virtual InnerT get(int index);
virtual void set(int index, InnerT item);
}
}
Run Code Online (Sandbox Code Playgroud)
我现在正在尝试声明IList的默认索引器.
我的猜测是这样的:
property Object^ System::Collections::IList::default[int]{
virtual Object^ System::Collections::IList::get(int index);
virtual void System::Collections::IList::set(int index, Object^ item);
}
Run Code Online (Sandbox Code Playgroud)
但那只是给了我
错误C2061:语法错误:标识符'default'
任何提示?
我不确定发生了什么.我有以下基类:
public class MyRow : IStringIndexable, System.Collections.IEnumerable,
ICollection<KeyValuePair<string, string>>,
IEnumerable<KeyValuePair<string, string>>,
IDictionary<string, string>
{
ICollection<string> IDictionary<string, string>.Keys { }
}
Run Code Online (Sandbox Code Playgroud)
然后我有这个派生类:
public class MySubRow : MyRow, IXmlSerializable, ICloneable,
IComparable, IEquatable<MySubRow>
{
public bool Equals(MySubRow other)
{
// "MyRow does not contain a definition for 'Keys'"
foreach (string key in base.Keys) { }
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我会收到这个错误?"'MyNamespace.MyRow'不包含'Keys'的定义".这两个类都在MyNamespace命名空间中.我试过访问this.Keys,base.Keys并且都没有从内部工作MySubRow.我尝试将Keys属性标记为public,MyRow但得到"修饰符'公共'对此项无效",我认为因为有必要实现一个接口.
我相信这很愚蠢,我有点不好意思问这个问题,但我仍然找不到答案:
我正在看List<T>这个实现的课程IList.
public class List<T> : IList
Run Code Online (Sandbox Code Playgroud)
Ilist中包含的方法之一是
int Add(object value)
Run Code Online (Sandbox Code Playgroud)
我明白不List<T>应该暴露那种方法(类型安全...),它实际上没有.但它怎么可能呢?mustnt类实现整个接口?
如果查看只读集合的代码,它没有"添加"方法,而是定义ICollection<T>.Add(T Value)方法(显式接口实现).
当我用我的ReadOnlyDictionary类做了类似的事情时,FxCop 10抱怨我打破了CA1033.
public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
//CA1033 ERROR
void IDictionary<TKey, TValue>.Add(TKey, TValue) { //Throw Exception }
}
public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
//NO CA1033 ERROR
Add(TKey, TValue) { //Throw Exception }
}
Run Code Online (Sandbox Code Playgroud)
ReadOnlyCollectionClass:
public class ReadOnlyCollection<T> : ICollection<T>
{
void ICollection<T>.Add(T item) { //Throw Exception }
}
Run Code Online (Sandbox Code Playgroud)
那么,这是误报吗?微软的基本代码是坏的吗?是什么赋予了?
我正在尝试实现一个非常简单的接口,并使用它来访问一个类型的属性,该属性本身通过如下接口访问:
interface ITest
{
IOther Other { get; }
}
interface IOther { }
class Other : IOther { }
class Test : ITest
{
public Other Other { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下构建错误:
Error 13 'Test' does not implement interface member 'ITest.Other'. 'Charger.Shared.Test.Other' cannot implement 'ITest.Other' because it does not have the matching return type of 'IOther'.
Run Code Online (Sandbox Code Playgroud)
现在我明白错误在说什么,但我不明白为什么.'其他'实施'IOther'所以问题为何?
一个不完美的解决方案是明确实现接口:
class Test : ITest
{
public Other Other { get; set; }
IOther ITest.Other
{
get { return this.Other; } …Run Code Online (Sandbox Code Playgroud) 在Fortran中,我需要一个派生类型中的过程指针,它可以指向几个子例程中的一个.这个问题似乎在SO上很常见:
仅举几例.第一个参考文献非常好地提供了这个函数问题的答案.
但是,在类型绑定过程指针指向子例程的情况下,我仍然不清楚开发此类代码的方法.困难似乎是没有与返回的内容相关联的类型(因为没有真正"返回").
我还想指出的是,虽然一个简单的解决方案可以在FORTRAN的一个较新的标准(2003,2008)的存在,这种解决方案可能无法在所有的编译器,这可能是未来工作的问题的细微差别.所以我对编译器友好的解决方案很感兴趣.
我有一个当前有效的小代码(如下所示),但在我的大代码中,我在文件中遇到内部编译器错误(也显示如下),我在派生类型中使用过程指针.我的问题是:我可以对下面的代码做些什么
1)严格使用显式接口
2)最大化传递给编译器的信息
3)确保代码在尽可能多的编译器之间可移植(即使用fortran 90/95标准).
在多大程度上可以满足上述要求(1是最重要的)?是否有可能满足上述所有标准?我知道"满足所有这些标准"是主观的,但我认为对于关于函数而不是子例程的相同问题,答案是'是'.
gcc version 5.1.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project)
Run Code Online (Sandbox Code Playgroud)
小代码:
module subs_mod
implicit none
public :: add,mult
contains
subroutine add(x,y,z)
implicit none
integer,intent(inout) :: x
integer,intent(in) :: y,z
x = y+z
end subroutine
subroutine mult(x,y,z)
implicit none
integer,intent(inout) :: x
integer,intent(in) :: y,z
x = y*z
end subroutine
end module
module type_A_mod
use subs_mod
implicit none
public :: type_A,init,operate
type type_A
procedure(),pointer,nopass :: …Run Code Online (Sandbox Code Playgroud) 我的目标语言是带有.net框架的C#.我想知道这个主题背后的意义或原因是什么?
任何建议和建议都会得到高度赞赏.
编辑
为什么我问这个问题?因为现在,Array类的一些有用的成员就像索引一样在演员背后!我想知道如果微软分裂ilist接口会更好吗?
.net arrays interface explicit-interface explicit-implementation
c# ×6
interface ×5
.net ×1
arrays ×1
c#-2.0 ×1
c++-cli ×1
casting ×1
fortran ×1
fxcop ×1
indexer ×1
inheritance ×1
powershell ×1
procedure ×1
subroutine ×1
xml-comments ×1