覆盖图书馆项目中的方法 [英] Overriding methods from Library Project

查看:28
本文介绍了覆盖图书馆项目中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Windows Phone 8 应用程序.

I am working on Windows Phone 8 application.

我有一个 Windows Phone 类库项目(称为基础)和 Windows Phone 应用程序(称为子项).

I have a Windows Phone Class Library project(Called Base) and Windows Phone App(called child).

在我的 Base 项目中,我有一个包含方法和静态变量的类.

In my Base project i have a class which has methods and static variables.

例如:

 Class Settings
    {

    public static string AppName = "Base";

    public virtual List<String> TabDetails()
            {
                List<String> TabDetails = new List<string>();
                helpTabDetails.Add("AAA");
                helpTabDetails.Add("BBB");
                return helpTabDetails;
            }

    } 

现在在我的子应用程序中,我只是链接这个类(通过在拖放时按 ALT 我们可以链接类图像等......我没有在引用文件夹中添加引用).

Now in my child app i am just linking this class (by pressing ALT when we are dragging and dropping we can link the class image etc ... i am not adding a reference in th references folder).

现在在我的 Base 中,这些是我的应用程序的默认设置,但如果需要,我想更改这些设置,如果我不需要更改,则应将默认设置应用于我的子应用程序.

Now in my Base those were the default settings of my app, but i want to change those settings if REQUIRED, if i DONT REQUIRE to change then the default settings should be applied to my child app.

注意:My Base 将包含与我的应用相关的所有类,但我的子应用将仅包含应用特定的设置类.

NOTE: My Base will have all the class related to my app, but my child app will only contain the app specific settings class.

我怎样才能做到这一点?

How can i acheive this ?

编辑

更具体:

在我的基地:

 Class Demo : Settings
    {

    AppName = "XYX"

   override TabDetails() and give new implemntation

    }

现在在我的 Child 应用程序中,如果我覆盖这些静态变量或方法,那么它应该选择这些值,但如何实现?

Now in my Child app if i am OVERRIDING these static variables or methods then it should pick those values, but how to acheive that ?

推荐答案

好的,经过长时间的交谈,终于得到了 OP 正在寻找的要求.

Ok so after a long chat, Finally got to get the requirement the OP was looking for.

简单来说:

  • 他有一个名为AppSettings
  • 的界面
  • 一个名为DefaultSettings的类实现了这个接口
  • 另一个名为 CustomSettings 的类可以或不能出现在项目中,它也将实现 AppSettings 接口
  • He had an interface called AppSettings
  • A class called DefaultSettings implements this interface
  • Another class called CustomSettings could or could not be present in the project which also would implement the AppSettings interface

问题是如何检查和使用 ChildSettings 如果存在其他使用 DefaultSettings

Question is how to check and use ChildSettings if present else use DefaultSettings

解决方案:

使用反射并检查类型是否存在以及是否可以使用它.

Use reflection and check for the type's presence and if available to use it.

AppSettings appSettings;
var customSettingsType = Type.GetType("POCChild.ChildSettings");
if (customSettingsType == null)
  appSettings = new DefaultSettings();
else
  appSettings = (AppSettings)Activator.CreateInstance(customSettingsType);

Debug.WriteLine(appSettings.getAppName());

其中 "POCChild.ChildSettings"Namespace.ClassName

这篇关于覆盖图书馆项目中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆