草庐IT

interface-design

全部标签

c# - 为什么将泛型显式转换为类类型有限制,而将泛型转换为接口(interface)类型没有限制?

在阅读Microsoft文档时,我偶然发现了这样一个有趣的代码示例:interfaceISomeInterface{...}classSomeClass{...}classMyClass{voidSomeMethod(Tt){ISomeInterfaceobj1=(ISomeInterface)t;//CompilesSomeClassobj2=(SomeClass)t;//Doesnotcompile}}这意味着您可以将泛型显式转换为接口(interface),但不能转换为类,除非您有约束。好吧,我仍然无法理解这一决定背后的逻辑,因为接口(interface)和类类型转换都会抛出异常

c# - 为什么将泛型显式转换为类类型有限制,而将泛型转换为接口(interface)类型没有限制?

在阅读Microsoft文档时,我偶然发现了这样一个有趣的代码示例:interfaceISomeInterface{...}classSomeClass{...}classMyClass{voidSomeMethod(Tt){ISomeInterfaceobj1=(ISomeInterface)t;//CompilesSomeClassobj2=(SomeClass)t;//Doesnotcompile}}这意味着您可以将泛型显式转换为接口(interface),但不能转换为类,除非您有约束。好吧,我仍然无法理解这一决定背后的逻辑,因为接口(interface)和类类型转换都会抛出异常

c# - 接口(interface)上的属性

我有一个接口(interface),它定义了一些带有属性的方法。这些属性需要从调用方法中访问,但我拥有的方法不会从接口(interface)中提取属性。我错过了什么?publicclassSomeClass:ISomeInterface{MyAttributeGetAttribute(){StackTracestackTrace=newStackTrace();StackFramestackFrame=stackTrace.GetFrame(1);MethodBasemethodBase=stackFrame.GetMethod();object[]attributes=methodB

c# - 接口(interface)上的属性

我有一个接口(interface),它定义了一些带有属性的方法。这些属性需要从调用方法中访问,但我拥有的方法不会从接口(interface)中提取属性。我错过了什么?publicclassSomeClass:ISomeInterface{MyAttributeGetAttribute(){StackTracestackTrace=newStackTrace();StackFramestackFrame=stackTrace.GetFrame(1);MethodBasemethodBase=stackFrame.GetMethod();object[]attributes=methodB

c# - 接口(interface)中的重载方法

我今天的问题:接口(interface)中的重载方法不好吗?您知道,“如果您不在乎,请省略参数,我们会找出默认值”这类重载方法。像那样:voidAdd(objectitem);voidAdd(objectitem,boolshouldDoSomething);voidAdd(objectitem,boolshouldDoSomething,IUltraObscureDeviceContextcontext);在这种情况下,我倾向于认为只有后者属于一个接口(interface),其他的应该在它之上的抽象类中实现。但话又说回来,我不确定。此外,有时您只是希望不同的重载执行略有不同的工作(如

c# - 接口(interface)中的重载方法

我今天的问题:接口(interface)中的重载方法不好吗?您知道,“如果您不在乎,请省略参数,我们会找出默认值”这类重载方法。像那样:voidAdd(objectitem);voidAdd(objectitem,boolshouldDoSomething);voidAdd(objectitem,boolshouldDoSomething,IUltraObscureDeviceContextcontext);在这种情况下,我倾向于认为只有后者属于一个接口(interface),其他的应该在它之上的抽象类中实现。但话又说回来,我不确定。此外,有时您只是希望不同的重载执行略有不同的工作(如

c# - 为什么在接口(interface)列表中的泛型内部使用私有(private)嵌套类型不是 "inconsistent accessibility"?

如果标题不是完全不言自明的,下面是令我困惑的代码:publicinterfaceIFoo{}publicclassMyClass:IFoo{privateclassNestedInMyClass{}}我很惊讶这个编译没有错误。感觉我在暴露一个private类型。这不应该是违法的吗?也许您的答案只是“没有反对的规则,那为什么不可以呢?”也许同样令人惊讶的是MyClass.NestedInMyClass甚至在“范围内”。如果我删除MyClass.资格,它不会编译。(如果我将IFoo更改为通用类,它应该成为MyClass的基类,这是非法的,因为基类型必须是至少与类型本身一样易于访问。)我用V

c# - 为什么在接口(interface)列表中的泛型内部使用私有(private)嵌套类型不是 "inconsistent accessibility"?

如果标题不是完全不言自明的,下面是令我困惑的代码:publicinterfaceIFoo{}publicclassMyClass:IFoo{privateclassNestedInMyClass{}}我很惊讶这个编译没有错误。感觉我在暴露一个private类型。这不应该是违法的吗?也许您的答案只是“没有反对的规则,那为什么不可以呢?”也许同样令人惊讶的是MyClass.NestedInMyClass甚至在“范围内”。如果我删除MyClass.资格,它不会编译。(如果我将IFoo更改为通用类,它应该成为MyClass的基类,这是非法的,因为基类型必须是至少与类型本身一样易于访问。)我用V

c# - 自定义 ASP.NET Identity 2.0 UserStore - 是否需要实现所有接口(interface)?

我创建了一个自定义IUserStore为我的申请。我已经实现了我需要的接口(interface),IUserStore,IUserRoleStore,IUserLockoutStore,IUserPasswordStore但是当我打电话的时候varresult=awaitSignInManager.PasswordSignInAsync(model.UserName,model.Password,model.RememberMe,shouldLockout:false);我得到一个异常(exception)说StoredoesnotimplementIUserTwoFactorStor

c# - 自定义 ASP.NET Identity 2.0 UserStore - 是否需要实现所有接口(interface)?

我创建了一个自定义IUserStore为我的申请。我已经实现了我需要的接口(interface),IUserStore,IUserRoleStore,IUserLockoutStore,IUserPasswordStore但是当我打电话的时候varresult=awaitSignInManager.PasswordSignInAsync(model.UserName,model.Password,model.RememberMe,shouldLockout:false);我得到一个异常(exception)说StoredoesnotimplementIUserTwoFactorStor