- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个基类 Foo,它有一个属性,用于从数据库项中识别主键。其他类从我的基类 Foo 派生而来,并包含这些类的特殊性的另一个属性。每个后代类代表数据库中的一个不同表。
通常,当加载这些数据时,这些项目存储在类型为 List
当我需要使用某些项目时,我会根据主键找到它。
当这些项目具有相同的主键时,我的问题就出现了,当查看派生类 FooB 中的项目最终找到类 FooA 时,因为这是最先出现的,然后执行转换为 FooB 的地方会引发异常。
var list = new List<Foo>();
list.Add(new FooA() { PrimaryKey = 1 });
list.Add(nwe FooB() { PrimaryKey = 1 });
public Foo FindItem(int pk)
{
return list.First(it => it.PrimaryKey == 1);
}
在很多地方我都是用这种方法来根据自己的需要找到我的元素。
var item = (FooB)FindItem(1);
在其他类中:
var item = (FooA)FindItem(1);
在我的例子中,这段代码在系统中的许多地方使用,将 List
我需要一个不改 rebase 本列表的解决方案。
最佳答案
您需要重写 FindItem(1)
方法不返回 Foo
的一个实例而是一个包含 FooA
的特殊对象带有 PrimaryKey==1
的实例和 FooB
带有 PrimaryKey==1
的实例.
第二步是提供转换运算符,将这个特殊对象转换为您需要的类型,以便代码 var item = (FooB)FindItem(1)
将按预期工作。
下面是实现此类解决方案的完整工作代码。主要缺点是您必须为正在使用的每个派生类型提供转换运算符实现。您也许还可以得出 FooWrapper
来自 Foo
(在这种情况下,Foo
中只有一个属性会非常简单,但这取决于您实际的 Foo
类)。
using System;
using System.Collections.Generic;
using System.Linq;
namespace FooWrapper
{
class Program
{
private static List<Foo> list = new List<Foo>();
static FooWrapper FindItem(int id)
{
return new FooWrapper(list.Where(o => o.PrimaryKey == id));
}
static void Main(string[] args)
{
list.Add(new FooA() { PrimaryKey = 1, Title = "aaa" });
list.Add(new FooB() { PrimaryKey = 1, Age = 123 });
// test the implicit cast to Foo type. this prefers the FooA type.
Foo item = FindItem(1);
Console.WriteLine(item.PrimaryKey + " - " + item.GetType().Name);
// retrieve the FooWrapper and cast it to FooA type.
var itema = (FooA)FindItem(1);
Console.WriteLine(itema.PrimaryKey + " - " + itema.Title + " - " + itema.GetType().Name);
// retrieve the FooWrapper and cast it to FooB type. The wrapper instance
// retrieved is the same, but the cast retrieves the correct type
var itemb = (FooB)FindItem(1);
Console.WriteLine(itemb.PrimaryKey + " - " + itemb.Age + " - " + itemb.GetType().Name);
}
}
public sealed class FooWrapper
{
private FooA _a;
private FooB _b;
public FooWrapper(IEnumerable<Foo> items)
{
FooA a;
FooB b;
foreach (var i in items)
{
a = i as FooA;
if (a != null)
{
this._a = a;
}
else
{
b = i as FooB;
if (b != null)
this._b = b;
}
}
}
public static implicit operator Foo(FooWrapper obj)
{
if (obj == null)
return null;
return obj._a == null ? (Foo)obj._b : obj._a;
}
public static explicit operator FooA(FooWrapper obj)
{
if (obj == null)
return null;
return obj._a;
}
public static explicit operator FooB(FooWrapper obj)
{
if (obj == null)
return null;
return obj._b;
}
}
public abstract class Foo
{
public int PrimaryKey { get; set; }
}
public class FooA : Foo
{
public string Title { get; set; }
}
public class FooB : Foo
{
public int Age { get; set; }
}
}
关于c# - 从基本类型列表中返回正确的后代类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16691804/
如何将十进制数字转换为mixed radix表示法? 我猜想给定每个基数数组的输入和十进制数,它应该输出每列值的数组。 最佳答案 伪代码: bases = [24, 60, 60] input = 8
我有 Table-A,其中有“x”行。 (对于这个例子有 8 行) 我通过使用游标创建了列数为“x”的Table-C。 (使其动态化;如果将更多行添加到 Table-A,则会在 Table-C 中创建
我有一个关于对象的(很可能是简单而愚蠢的)问题。我创建了实例“Person”的对象“jon”。当我打电话时 console.log(jon.name) 控制台会给我输出“jon”。到目前为止,一切都很
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: javascript function vs. ( function() { … } ()); 抱歉,如果这太基础了
我正在尝试用 Java 重新创建射弹轨迹,但是,我遇到了一些问题。我看过很多解释公式之类的视频,但他们的方程中有一个目标,而我没有。我的意思是,他们有一个范围来计算子弹的下落,但我试图弄清楚子弹最终会
(希望如此)来自一个完整的 Rust 初学者的一个简单问题。我的循环有什么问题? num 计算结果为“69”的速度相当快,但是一旦 num 设置为“69”,循环就永远不会退出。我肯定遗漏了一些明显的东
我在 id="name"的元素上应用“.length”,但它计数为 29 而不是 14。我想知道我的错误在哪里?如果有人可以让我知道,那就太好了。谢谢! var name=document.getEl
我知道这很简单,但由于某种原因我无法让它工作。我正在尝试在 Java 中创建自定义颜色,但它似乎不起作用。 import java.awt.Color; Color deepGreen = new C
我有一个大文件,其中每一行都包含一个子字符串,例如 ABC123。如果我执行 grep ABC file.txt 或 grep ABC1 file.txt 我按预期返回这些行,但如果我执行 grep
我想将以下实体映射转换为 Priority 对象。在 getter 上,当我将“Short”更改为“Priority”并遵循 this.priority 时,它会提示 'basic' 属性类型不应该是
我正在开发一个相当基本的函数,我发现很难弄清楚为什么我会得到我的输出。 def mystery(n): print(n) if n < 4: my
我正在尝试对 WordPress 安装的新闻部分实现同位素过滤。我是 JavaScript/jQuery 的新手,正在尝试随时随地学习。我首先使用 Filters section of the Iso
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我在另一个实体类中引用一个实体并收到此错误。下面是示例代码。我在 persistence.xml 中也有这些类。 是什么导致了这个问题?我正在使用 Spring 数据 JPA 和 Hibernate。
我正在解析 HTML 并重新格式化图像以使其更好地适应。由于某种原因,当我有多个图像需要解析时,我会超出范围,而且我一生都无法弄清楚为什么。 当 imgArray.count >1 时,我将使用带有递
我是 SQL 新手,正在尝试创建一个基本的子查询。我需要找出经理的平均年龄和实习生的平均年龄之间的差异。 标题为一栏 - 经理或实习生年龄是一列,全部在同一个表中。 我会使用两个子查询来做类似的事情:
我习惯了 csh,所以不得不使用 bash 有点烦人。这段代码有什么问题? if[$time > 0300] && [$time 和 300 && time < 900 )) then mod
我建立了这个页面:http://excelwrestling.com/poola.php即将到来的双重锦标赛。我的大部分数据都是从我的 mySQL 数据库中提取的,现在只有一些示例数据。 我希望链接选
是否有任何原因导致以下内容不起作用: for (i=0;i < someArray.length;i++) { if (someArray[i].indexOf("something") !=
我现在正在学习 Javascript,有一个问题一直困扰着我! 因此,我在这里所需要做的就是在此输入框中键入颜色,单击按钮并将标题更改为键入的颜色(仅当键入的颜色位于变量中指定的数组中时)。 我的代码
我是一名优秀的程序员,十分优秀!