简体   繁体   English

C#中的条件类继承

[英]Conditional Class inheritance in C#

Alright, so I'm developing an app for Windows 8.1 Universal and there's some API's on the phone that doesn't exist on the PC Platform. 好吧,所以我正在为Windows 8.1 Universal开发一个应用程序,并且手机上有一些API在PC平台上不存在。 The thing is I'm trying to Inherit a class conditionally if the current platform is the windows phone. 问题是,如果当前平台是Windows Phone,我将尝试有条件地继承类。 here's a snippet of my code (THAT DOES NOT WORK) 这是我的代码的一部分(不起作用)

    public class Client : IDisposable, IClient
#if WINDOWS_PHONE_APP
        , IWebAuthenticationContinuable
#endif
    {
#if WINDOWS_PHONE_APP
        void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args) { }
#endif

        public void DoStuff()
        {

        }

        public void Dispose()
        {

        }
    }

whenever i try to create a new instance of this class in my view model i get the following error: Cannot create instance of type 'App87.ViewModels.MainViewModel' [Line: 9 Position: 27] 每当我尝试在视图模型中创建此类的新实例时,都会出现以下错误:无法创建类型为'App87.ViewModels.MainViewModel'的实例[行:9位置:27]

Line 9 is my constructor which only creates a new instance of the Client class. 第9行是我的构造函数,它仅创建Client类的新实例。

Tried your exact code, there is only one thing that isn't working: ContinueWebAuthentication should be marked as public since it's inherited from an interface: 尝试了确切的代码,只有一件事不起作用: ContinueWebAuthentication应该被标记为public,因为它是从接口继承的:

public interface IClient { }

#if WINDOWS_PHONE_APP
public interface IWebAuthenticationContinuable
{
    void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args);
}
#endif

public class Client : IDisposable, IClient
#if WINDOWS_PHONE_APP
    , IWebAuthenticationContinuable
#endif
{
#if WINDOWS_PHONE_APP
    public void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args) { }
#endif

    public void DoStuff()
    {

    }

    public void Dispose()
    {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM