使用通用接口属性覆盖非泛型接口属性(Overriding a non-generic interface property with a generic one)
我有两个接口,如下所示:
public interface IEntityViewModel<T> : IEntityViewModel where T : class, ICLEntity { new T Entity { get; set; } } public interface IEntityViewModel { void LoadEntity(int primaryKey); bool? DialogResult { get; set; } ICLEntity Entity { get; set; } }
当我实现一个类时,如何确保该类上出现的Entity属性是通用T? 这是一个示例类:
public abstract class EntityConductor<T> : IEntityViewModel<T> where T : class, ICLEntity { public T Entity { get; set; } }
编译器要求我也添加IEntityViewModel实现,例如:
ICLEntity IEntityViewModel.Entity { get { return ActiveItem.Entity; } set { ActiveItem.Entity = value as T; } }
但后来我在课堂上得到了两个Entity属性,这不是我需要的。 如果实现泛型接口,我需要将Entity属性设置为T类型,如果是非泛型,则需要为ICLEntity类型。
如何才能做到这一点? 我错过了一些简单的事吗? 我使用的是.NET 4.0,协方差可以帮助我吗?
谢谢!
I have two interfaces like the following:
public interface IEntityViewModel<T> : IEntityViewModel where T : class, ICLEntity { new T Entity { get; set; } } public interface IEntityViewModel { void LoadEntity(int primaryKey); bool? DialogResult { get; set; } ICLEntity Entity { get; set; } }
When I implement a class, how can I ensure that the Entity property that appears on the class is the generic T? Here's a sample class:
public abstract class EntityConductor<T> : IEntityViewModel<T> where T : class, ICLEntity { public T Entity { get; set; } }
The compiler requires I add the IEntityViewModel implementation as well, for example:
ICLEntity IEntityViewModel.Entity { get { return ActiveItem.Entity; } set { ActiveItem.Entity = value as T; } }
but then I get two Entity properties in the class, which is not what I need. If implementing the generic interface, I need the Entity property to be of type T, and if the non generic, to be of type ICLEntity.
How can this be done? Am I missing something simple? I am using .NET 4.0, can covariance help me here?
Thanks!
原文:https://stackoverflow.com/questions/6604503
最满意答案
您可以使用
magrittr
和dplyr
管道运算符(%>%
)将数据传递到ggplot调用。cars %>% {ggplot(., aes(speed, dist)) + geom_point() + coord_cartesian(xlim = c(min(.$speed), max(.$speed) + 10)) }
请注意,现在需要使用
.
在ggplot调用开始时显式标识data参数.
代词。You can use the
magrittr
anddplyr
pipe operator (%>%
) to pass data into the ggplot call.cars %>% {ggplot(., aes(speed, dist)) + geom_point() + coord_cartesian(xlim = c(min(.$speed), max(.$speed) + 10)) }
Note that now you need to explicitly identify the data argument at the beginning of the ggplot call using the
.
pronoun.
相关问答
更多-
这是一个通过编辑用于绘制置信区间的数据的解决方案(提取数据的方法是从仅绘制ggplot2中stat_smooth的边界借来的 ) 首先,我们创建我们的正常情节: p1 <- ggplot(df, aes(date, Y)) + geom_point() + geom_smooth(level = 0.99) + theme_bw() 然后,我们提取更平滑的数据并编辑ymin变量 smooth_data <- ggplot_build(p1)$data[[2]] smooth_data$ymin ...
-
使用coord_cartesian进行缩放后,向ggplot添加刻度(Add ticks to ggplot after zooming with coord_cartesian)[2021-12-08]
您仍然可以将scale_y_continuous与coord_cartesian scale_y_continuous使用 + scale_y_continuous(breaks = seq(0,50, by=5)) You can still use scale_y_continuous with coord_cartesian + scale_y_continuous(breaks = seq(0,50, by=5)) -
将您的代码更改为: ggplot(data=iris2, aes(x=Sepal.Length, y=Sepal.Width, fill=Sepal.Length)) + ylim(c(5000, 150000)) +geom_bar(stat="identity") 你只需要ylim,coord_cartesian就没有必要了 Change your code to: ggplot(data=iris2, aes(x=Sepal.Length, y=Sepal.Width, fill=Sepal.Len ...
-
ggplot如何在“mask”剪切后显示没有NA值的光栅?(ggplot How to display a raster without the NA values after a cut with “mask”?)[2021-10-15]
看起来scale_fill_manual在使用因子时显示NA值。 定义您真正想要展示的因素的一种方法是使用其他参数breaks和带有对应向量的labels 。 如果我使用你的代码,这将是: # Vector of correspondence cols <- c("A" = "red", "B" = "#22751a", "C" = "#48c665", "D" = "#d3d532", "E" = "#d78d0d", "F" = "#f6e600","G" = "#65d6ef") ... -
如何不使用ggplot2在coord_polar中显示值0(How NOT to display value 0 in a coord_polar using ggplot2)[2023-02-22]
不要将数据subset 。 使用ifelse : ggplot(c, aes(x=0.5, y=b, fill=a)) + geom_bar(stat="identity", color="white", width=1) + coord_polar(theta='y') + theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), ... -
一种方法是在x轴上绘制两个图表,并设置factor(week) ,但在g2图(步骤图)中,在geom_blank()这样做,以便设置比例。 然后在geom_step() ,以数字刻度绘图: as.numeric(factor(week)) library(ggplot2) library(grid) # Your data var1 <- data.frame(val = rnorm(300), week = c(rep(25, 100), ...
-
您可以使用magrittr和dplyr管道运算符( %>% )将数据传递到ggplot调用。 cars %>% {ggplot(., aes(speed, dist)) + geom_point() + coord_cartesian(xlim = c(min(.$speed), max(.$speed) + 10)) } 请注意,现在需要使用.在ggplot调用开始时显式标识data参数. 代词。 You can use the magrittr and dplyr pi ...
-
我不确定,但我想离散比例只接受字符作为限制参数,每个字符代表一个因子级别(参见http://docs.ggplot2.org/0.9.3.1/discrete_scale.html )。 df <- data.frame(x=sample(1:3, 10, replace=TRUE), y=sample(20:30, 10, replace=TRUE)) df$x <- as.factor(df$x) cfr <- ggplot(df, aes(x = x,y = y)) + geom_line(siz ...
-
如果我确实理解了什么是问题: 那么,有没有办法可以覆盖此请求的RouteValueDictionary? 还是有其他方法我可以做到这一点,我错过了? 我已经采取了您的解决方案并更改了这些行来替换参数名称: for (int i = 0; i < methodParameters.Length; i++) { // I. instead of this //values.First(x => x.Key == unMappedList.ElementAt(i).Key) // ...
-
对于ylimits,您可以添加: + scale_y_continuous(limits=c(0,500)) 为了设置刻度点,您可以使用: breaks=seq(0, 500, 50) # which means ticks from 0-500, every 50 units 因此,我认为您需要将以下代码添加到您的绘图代码中: +scale_y_continuous(limits=c(0,500),breaks=seq(0, 500, 50)) For the ylimits you can a ...