我试图找到使用通用接口通用列表作为变量的正确方法.
这是一个例子.这可能不是最好的,但希望你能明白这一点:
public interface IPrimitive<T>
{
T Value { get; }
}
Run Code Online (Sandbox Code Playgroud)
然后在另一个类中,我希望能够声明一个包含实现IPrimitive<T>任意对象的列表的变量T.
// I know this line will not compile because I do not define T
List<IPrimitive<T>> primitives = new List<IPrimitives<T>>;
primitives.Add(new Star()); // Assuming Star implements IPrimitive<X>
primitives.Add(new Sun()); // Assuming Sun implements IPrimitive<Y>
Run Code Online (Sandbox Code Playgroud)
需要注意的是,T在IPrimitive<T>可能是列表中的每个条目不同.
关于如何建立这种关系的任何想法?替代方法?
泽西1.6可以生产:
@Path("/stock")
public class StockResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Stock> get() {
Stock stock = new Stock();
stock.setQuantity(3);
return Lists.newArrayList(stock);
}
}
Run Code Online (Sandbox Code Playgroud)
但不能做同样的事情:
@Path("/stock")
public class StockResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response get() {
Stock stock = new Stock();
stock.setQuantity(3);
return Response.ok(Lists.newArrayList(stock)).build();
}
}
Run Code Online (Sandbox Code Playgroud)
给出错误: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/json was not found
这可以防止使用HTTP状态代码和标头.
请考虑以下代码示例:
TempList.ForEach(Function(obj)
obj.Deleted = True
End Function)
Run Code Online (Sandbox Code Playgroud)
还有这个:
TempList.ForEach(Function(obj) obj.Deleted = True)
Run Code Online (Sandbox Code Playgroud)
我希望结果是相同的,但第二个代码示例不会更改列表TempList中的对象.
这篇文章更能理解为什么......?或者至少得到一些帮助,了解为什么......
我有一个拥有通用列表的类Team:
[DataContract(Name = "TeamDTO", IsReference = true)]
public class Team
{
[DataMember]
private IList<Person> members = new List<Person>();
public Team()
{
Init();
}
private void Init()
{
members = new List<Person>();
}
[System.Runtime.Serialization.OnDeserializing]
protected void OnDeserializing(StreamingContext ctx)
{
Log("OnDeserializing of Team called");
Init();
if (members != null) Log(members.ToString());
}
[System.Runtime.Serialization.OnSerializing]
private void OnSerializing(StreamingContext ctx)
{
Log("OnSerializing of Team called");
if (members != null) Log(members.ToString());
}
[System.Runtime.Serialization.OnDeserialized]
protected void OnDeserialized(StreamingContext ctx)
{
Log("OnDeserialized of Team called");
if (members != null) Log(members.ToString()); …Run Code Online (Sandbox Code Playgroud) 在我的Visio 2007 UML文档中,我无法弄清楚如何向返回泛型List<MyCustomType>类型的接口添加操作.
例如:
假设我有一个名为"MyClass"的类和一个名为"IFace"的接口.IFace有一个返回MyClass通用列表的方法的签名.
为清楚起见,这是C#代码的示例:
namespace StackO
{
public interface IFace
{
List<MyClass> SomeMethod(string data);
}
public class MyClass
{
}
}
Run Code Online (Sandbox Code Playgroud)
这是我卡住的截图:
似乎将指定a List<MyClass>作为我的返回类型的唯一方法是创建另一个显式写为的用户定义数据类型List<MyClass>.如果是这种情况,那就这样吧.但是,我发布这篇文章是希望有更好/正确的方法来做到这一点.
如何将Visio接口操作的返回类型定义为用户定义数据类型的通用列表?
假设我有一个表示数据字段的对象,该对象需要以下属性:Name,Type,Value,Length.这是对象:
class Field<T>
{
public string Name { get; set; }
public Type Type
{
get
{
return typeof(T);
}
}
public int Length { get; set; }
public T Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我使用了泛型,因为我想强制代码的用户只能分配某种类型的值.
现在的问题是我想创建一个字段列表.
如果我像那样创建列表,List<Field<object>>我们可以将任何值分配给列表中的给定字段,当我们查询Type时,我们得到'object'.
事情是 - 在那个列表上我可能想要几个字段持有字符串,很少持有整数,日期,甚至自定义对象反过来将有一个字段列表......
泛型是一个很好的解决方案吗?如果是的话,我将如何实施呢?如果没有,有什么更好的方法?
---编辑---
只是为了增加一些背景:
1.我可能想要一个字段列表,每个字段将包含不同的数据类型,如下所示:
List<Field<object>> lst = new List<Field<object>>();
lst.Add(new Field<string>());
lst.Add(new Field<int>());
lst.Add(new Field<SomeObjectFromMyApp>());
Run Code Online (Sandbox Code Playgroud)
2.稍后我将不得不在循环中自动查询这些对象及其属性,类似于:
foreach(Field<object> fld in lst)
{
Type t = fld.Type;
//do some other stuff
}
Run Code Online (Sandbox Code Playgroud) 好吧所以我对单元测试相当新,到目前为止一切顺利.我在这里简化了我的问题,但基本上我有以下几点:
[Test]
public void ListTest()
{
var expected = new List<MyClass>();
expected.Add(new MyOtherClass());
var actual = new List<MyClass>();
actual.Add(new MyOtherClass());
Assert.AreEqual(expected,actual);
//CollectionAssert.AreEqual(expected,actual);
}
Run Code Online (Sandbox Code Playgroud)
但是测试失败了,不应该通过测试吗?我错过了什么?
我确信这个问题可以解决上一个问题中提出的问题,但我无法找到它.
C#类中有一个方法,它将基类的通用List作为参数.我需要传递一个继承类的列表,并且不知道如何执行此操作.我的尝试中出错了.以下是示例代码:
public class A
{
public static void MethodC(List<A>)
{
// Do Something here with the list
}
}
public Class B : A
{
// B inherits from A, A is the Base Class
}
// Code utilizing the above method
List<B> listOfB = new List<B>();
A.MethodC( (List<A>) listOfB ); // Error: this does not work
A.MethodC( listOfB.ToList<typeof(A)>() ); // Error: this does not work
A.MethodC( listOfB.ConvertAll<A>(typeof(A)) ); // Error: this does not work
// how can I …Run Code Online (Sandbox Code Playgroud) 如何在下面的代码中编辑列表中的项目:
List<Class1> list = new List<Class1>();
int count = 0 , index = -1;
foreach (Class1 s in list)
{
if (s.Number == textBox6.Text)
index = count; // I found a match and I want to edit the item at this index
count++;
}
list.RemoveAt(index);
list.Insert(index, new Class1(...));
Run Code Online (Sandbox Code Playgroud) 我的代码看起来像这样:
Collection<NameValueCollection> optionInfoCollection = ....
List<NameValueCollection> optionInfoList = new List<NameValueCollection>();
optionInfoList = optionInfoCollection.ToList();
if(_isAlphabeticalSoting)
Sort optionInfoList
Run Code Online (Sandbox Code Playgroud)
我尝试了optionInfoList.Sort()但它无法正常工作.