我附上了我正在尝试做的事情的图片。假设我有一个类(class)中的 T 列表
public class MyClass<T>
where T : IMyInterface
{
public List<T> list = new List<T>;
}
现在,另一个类有一个 MyClass 列表。
public class AnotherClass
{
public List<MyClass<IMyInterface>> list = new List<MyClass<IMyInterface>>;
}
我应该为 MyClass 输入什么 T?如果我输入 T,那么它会假定该类中的所有类型都相同,但事实并非如此。如果我放置 IMyInterface,则在访问这些类时无法将 IMyInterface 强制转换为 T。
new AnotherClass().list.Add(new MyClass<T>());
现在这里的类型是什么? AnotherClass 泛型类型中的列表是 IMyInterface,但我要添加的是 T,我希望它是动态的,但它们仍在 IMyInterface 下。
最佳答案
泛型中的 T 在其整个生命周期内必须相同。
举个例子
var a = new MyClass<int>();
a.list.Add(1); //since a's generic T is declared as int the list is now also int
var b = new MyClass<string>()
b.list.Add("string"); //in b its now declared as a string and avccepts only string
所以当你这样做的时候。
var c = new MyClass<IMyInterface>();
c.list.Add(ObjectImplementingIMyInterface); //You must add something that implements IMyInterface
关于内容你唯一知道的是它实现了 IMyInterface 现在如果你想拆分对象的执行那么你必须通过反射检查对象的类型。
if(c.list[i].GetType() == typeof(ClassA_IMyInterface)
{
//Execute on ClassA_IMyInterface
//If you are after the ClassA
//and want to run something speciffic for it the cast
ClassA clA = = (ClassA)c.list[i];
//Now you have access to the implementation of ClassA instead of just the interface
}
else if (c.list[i].GetType() == typeof(ClassB_IMyInterface)
{
//Execute on ClassB_IMyInterface
}
这是我在 ConsoleApplication 中制作的示例,展示了它的布局。
public class MyClass<T> where T : IMyInterface
{
public List<T> list = new List<T>();
}
public interface IMyInterface
{
}
public class Foo : IMyInterface
{
}
public class Bar : IMyInterface
{
}
public class FooBar
{
public void Test()
{
var content = new MyClass<IMyInterface>();
content.list.Add(new Foo());
if (content.list[0] is Foo)
{
//Execute on Foo
var g = (Foo)content.list[0];
//Now you can access Foo's methods and not only the Interfaces
Console.WriteLine("Foo");
}
else if (content.list[0] is Bar)
{
//Execute on Bar
var g = (Bar)content.list[0];
//Now you can access Bar's methods and not only the Interfaces
Console.WriteLine("Bar");
}
}
}
关于c# - 在类中存储不同泛型类型的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36467888/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我