覆盖一个属性与派生类型和相同名称的C# [英] Override a Property with a Derived Type and Same Name C#

查看:150
本文介绍了覆盖一个属性与派生类型和相同名称的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重写基类的属性有不同,但衍生的同名类型。 ?我认为它可能通过的协变性或仿制药,但我不知道该怎么做。



下面的代码获取错误:




错误1'Sun.Cache':类型必须是
'OuterSpace.Cache'来匹配重写
会员'OuterSpace.Cache




 公共类外太空{
公共虚拟OuterSpaceData数据{搞定;设置;}
公共虚拟OuterSpaceAnalysis分析{搞定;设置;}
公共虚拟OuterSpaceCache缓存{搞定;设置;}


公共类OuterSpaceData {
//很多基本数据提取套路如
公共虚拟的GetData();
}
公共类OuterSpaceAnalysis {
//很多通用分析数据的例程例如
公共虚拟GetMean();
}
公共类OuterSpaceCache {
//过去很多分析缓存的结果:
公众解释< AnalysisType,列表与LT;结果>>了ResultCache;
}
}

公共类太阳:外太空{
公众覆盖SunData数据{搞定;设置;}
公众覆盖SunAnalysis分析{搞定;设置;}
公众覆盖SunCache缓存{搞定;设置;}

公共SunData:OuterSpaceData {
//例程的具体数据来自太阳例如
公众覆盖的GetData();
}

公共SunAnalysis:OuterSpaceAnalysis {
//套路具体分析太阳:例如
公共双ReadTemperature();
}
公共SunCache:OuterSpaceCache {
//任何数据缓存的特定于Sun的分析
公众解释< AnalysisType,列表与LT;结果>> TempCache;
}
}

公共类月亮:外太空{}等

有关最终结果,当我解决太阳的数据对象,我不希望这是两个数据对象(继承和放大器;基类),但是当我试图重写属性它需要阳光变量是相同类型的基类。例如:

 太阳EarthSun =新的Sun()
EarthSun.Analyse()//在外太空分析保存结果孙缓存:

//现在尝试使用结果:
EarthSun.Cache [0] ...

非常相似,这但派生类型,而不是字符串数组:
http://stackoverflow.com/questions/1112458/c-member-variable-overrides-used-by-base-class-method



和这个答案并没有多大意义,对我说:
http://stackoverflow.com/questions/1123975/how-to-override-member-of-base-class-after-inheritance-in -c



或许,这意味着它是不可能的?
http://stackoverflow.com/questions/157119/c-可以-I-覆盖-与派生类型



帮助! :)周围的任何工作?


解决方案

这是不可能的,你想要的方式,完全是。



您可以创建一个同名的新属性:

 大众新SunData数据
{
{返回(SunData)base.Data; }
集合{base.Data =价值; }
}



这是不太一样的东西,但它可能是接近你 。重新将获得



另一种可能的方法是:

 公共SunData SunData {搞定;组; } 

公众覆盖OuterSpaceData数据
{
{返回SunData; }
集合{SunData =(SunData)值; }
}



这种方法的优点是,它并保证它是不可能的放东西在你的数据属性,它不是一个SunData。缺点是,你的数据属性不强类型,你有,如果你希望它静态类型为SunData使用SunData属性。



有是一个丑陋的方式获得两全其美,由于它是混乱的代码和难以牺牲理解之后 - 通过引入一个中间派生的类,它在基类数据属性密封覆盖'并重定向到一个不同的保护特性,用新名称,然后有最终的派生类中添加一个新的数据属性,然后调用中间财产。这真的是一个丑陋的黑客攻击,不过,即使我已经做到了,我不会推荐它。


I'm trying to override a property in a base class with a different, but derived type with the same name. I think its possible by covarience or generics but am not sure how to do it?

The following code gets the error:

Error 1 'Sun.Cache': type must be 'OuterSpace.Cache' to match overridden member 'OuterSpace.Cache'

public class OuterSpace {
    public virtual OuterSpaceData Data {get; set;}
    public virtual OuterSpaceAnalysis Analysis {get; set;}
    public virtual OuterSpaceCache Cache {get; set;}


    public class OuterSpaceData {
        //Lots of basic Data Extraction routines eg
        public virtual GetData();
    }
    public class OuterSpaceAnalysis {
        //Lots of Generic Analysis on Data routines eg
        public virtual GetMean();
    }
    public class OuterSpaceCache {
        //Lots of Caches of Past Analysis Results:
        public Dictionary<AnalysisType, List<Result>> ResultCache;
    }
}

public class Sun : OuterSpace {
    public override SunData Data {get; set;}
    public override SunAnalysis Analysis {get; set;}
    public override SunCache Cache {get; set;}

    public SunData : OuterSpaceData {
        //Routines to specific get data from the sun eg
        public override GetData();
    }

    public SunAnalysis : OuterSpaceAnalysis {
        //Routines specific to analyse the sun: eg
        public double ReadTemperature();
    }
    public SunCache : OuterSpaceCache {
        //Any data cache's specific to Sun's Analysis
        public Dictionary<AnalysisType, List<Result>> TempCache;
    }
}

public class Moon : OuterSpace {} etc.

For the end result, when I address the "Data" Object of Sun I don't want there to be two Data Objects (Inherited & Base Class) but when I try override the property it requires the Sun variables to be the same type as base class. Eg:

Sun EarthSun = new Sun()
EarthSun.Analyse()  //The OuterSpace Analysis Saves results to Sun Cache:

//Now try use the result:
EarthSun.Cache[0]...

Very similar to this but with derived type instead of string-array: http://stackoverflow.com/questions/1112458/c-member-variable-overrides-used-by-base-class-method

And this answer didn't make much sense to me: http://stackoverflow.com/questions/1123975/how-to-override-member-of-base-class-after-inheritance-in-c

Or perhaps this means its just not possible? http://stackoverflow.com/questions/157119/c-can-i-override-with-derived-types

Help! :) Any work around?

解决方案

It is not possible the way you want it, exactly.

You can create a "new" property with the same name:

public new SunData Data
{
  get { return (SunData) base.Data; }
  set { base.Data = value; }
}

It's not quite the same thing, but it's probably as close as you're going to get.

Another possible approach would be:

public SunData SunData { get; set; }

public override OuterSpaceData Data
{
  get { return SunData; }
  set { SunData = (SunData)value; }
}

The advantage of this approach is that it does guarantee that it's impossible to put something in your Data property that is not a SunData. The downside is that your Data property is not strongly typed and you have to use the SunData property if you want it statically typed to SunData.

There's an ugly way to get the "best of both worlds" at the expense of being confusing to code and difficult to understand afterwards - by introducing an intermediate derived class that does a 'sealed override' on the base class Data property and redirects to a different protected property with a new name, and then have the ultimate derived class add a 'new' Data property that then calls the intermediate property. It really is an ugly hack, though, and even though I've done it, I wouldn't recommend it.

这篇关于覆盖一个属性与派生类型和相同名称的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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