给定一个结构MyStruct,我可以sizeof(MyStruct)在不安全的代码中使用该结构的实例大小.但是,我想获得给定结构Type对象的结构的大小,即sizeof(typeof(MyStruct)).有Marshal.SizeOf,但是返回非托管编组大小,而我想要该结构的托管大小.
我最近使用过LINQ
在以下代码中:
ArrayList list = new ArrayList();
var myStrings = list.AsQueryable().Cast<string>();
Run Code Online (Sandbox Code Playgroud)
有什么AsQueryable用?我知道Cast创建一个类型安全的集合,并且ArrayList已弃用.
我有一个朋友说他需要AsQueryable结合使用ArrayList.我试图理解为什么,但我不明白为什么AsQueryable需要.
他错了吗?
我正在考虑kafka实现一个低延迟的消息队列,我一直在阅读有关消费者长期轮询的信息.但是,没有关于如何实际使用长轮询的示例,或者您需要设置哪些选项来启用它.如何使用kafka java api启用长轮询?
我有一个带有Equals方法的集合类,我希望在方法中传递每个项目之间的相等性检查.此外,我想允许委托类型操作T的超类以及T本身:
public delegate bool EqualityComparer<T>(T x, T y);
public class Collection<T>
{
//...
public bool Equals<U>(Collection<T> other, EqualityComparer<U> eq) where T : U
{
// code using eq delegate to test equality between
// members of this and other collection
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,编译器对此进行了抨击('Collection.Equals()'没有定义类型参数'T').有没有办法指定这种类型的约束/操作?
在.NET结构设计指南中,它将结构的最大合理大小赋予16个字节.如何确定结构的大小,是否受运行程序的体系结构的影响?这个值仅为32位,还是两个拱门?
从具体子类中提取抽象类中的值的"正确"方法是什么?
即,我应该这样做:
abstract class A {
private string m_Value;
protected A(string value) {
m_Value = value;
}
public string Value {
get { return m_Value; }
}
}
class B : A {
B() : this("string value") {}
}
Run Code Online (Sandbox Code Playgroud)
或这个:
abstract class A {
protected A() { }
public abstract string Value { get; }
}
class B : A {
B() {}
public override string Value {
get { return "string value"; }
}
}
Run Code Online (Sandbox Code Playgroud)
或者是其他东西?
如果Value属性仅用于抽象类,是否应该做不同的事情?
以下代码不编译(error CS0123: No overload for 'System.Convert.ToString(object)' matches delegate 'System.Converter<T,string>'):
class A<T> {
void Method(T obj) {
Converter<T, string> toString = Convert.ToString;
// this doesn't work either (on .NET 4):
Converter<object, string> toString2 = Convert.ToString;
Converter<T, string> toString3 = toString2;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这样做:
class A<T> {
void Method(T obj) {
// o is a T, and Convert.ToString(o) is using
// string Convert.ToString(object o)
Converter<T, string> toString = o => Convert.ToString(o);
}
}
Run Code Online (Sandbox Code Playgroud)
在c#4中,可以将co/contra-variant代表彼此分配,并且可以使用co/contra-variant方法创建代理,因此该ToString(object)方法可以用作a Converter<T, string>,因为T …
我有类似的情况从其他Maven模块添加jar-with-dependencies工件,但是建议的解决方案对我不起作用.我已经尝试过使用moduleSets和dependencySets.所以我们走了:
我有一个多模块maven项目:
parent
|
+-myProject
|
+-myProjectTests
Run Code Online (Sandbox Code Playgroud)
我希望myProjectTests将jar-with-dependenciesmyProject生成在其输出目录中,以运行集成测试等(我使用的框架需要运行实际的jar)
parent pom.xml:
<project>
<groupId>myGroup</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<modules>
<module>myProject</module>
<module>myProjectTests</module>
</modules>
</project>
Run Code Online (Sandbox Code Playgroud)
myProject pom.xml:
<project>
<groupId>myGroup</groupId>
<artifactId>myProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</plugins>
</project>
Run Code Online (Sandbox Code Playgroud)
myProjectTests pom.xml:
<project>
<groupId>myGroup</groupId>
<artifactId>myProjectTests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>myGroup</groupId>
<artifactId>myProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>prepareTests</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors> …Run Code Online (Sandbox Code Playgroud) 我有这个方法(这是我原来问题的简化):
public List<AbstractMap.SimpleEntry<String, List<?>>> method(List<?> list) {
return Collections.singletonList(new AbstractMap.SimpleEntry<>("", list));
}
Run Code Online (Sandbox Code Playgroud)
但是,这会导致编译错误:
Run Code Online (Sandbox Code Playgroud)Console.java:40: error: incompatible types return Collections.singletonList(new AbstractMap.SimpleEntry<>("", list)); ^ required: List<SimpleEntry<String,List<?>>> found: List<SimpleEntry<String,List<CAP#1>>> where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ? 10 errors
如果我尝试在顶级方法上指定类型实例化:
return Collections.<AbstractMap.SimpleEntry<String, List<?>>>singletonList(new AbstractMap.SimpleEntry<>("", list));
Run Code Online (Sandbox Code Playgroud)
我得到一个不同的错误:
Run Code Online (Sandbox Code Playgroud)Console.java:40: error: method singletonList in class Collections cannot be applied to given types; return Collections.<AbstractMap.SimpleEntry<String, List<?>>>singletonList(new AbstractMap.SimpleEntry<>("", list)); ^ required: T found: SimpleEntry<String,List<CAP#1>> reason: actual argument SimpleEntry<String,List<CAP#1>> cannot be converted to SimpleEntry<String,List<?>> …
我有一个 node.js + express web 服务器,我正在用 Mocha 进行测试。我在测试工具中启动 Web 服务器,并连接到 mongodb 以查找输出:
describe("Api", function() {
before(function(done) {
// start server using function exported from another js file
// connect to mongo db
});
after(function(done) {
// shut down server
// close mongo connection
});
beforeEach(function(done) {
// empty mongo collection
});
describe("Events", function() {
it("Test1", ...);
it("Test2", ...);
it("Test3", ...);
it("Test4", ...);
it("Test5", ...);
});
});
Run Code Online (Sandbox Code Playgroud)
如果 Mocha 一次运行超过 4 个测试,则会超时:
4 passing (2s)
1 failing
1) Api Test5:
Error: …Run Code Online (Sandbox Code Playgroud) c# ×5
.net ×3
generics ×2
.net-4.0 ×1
apache-kafka ×1
architecture ×1
constraints ×1
covariance ×1
delegates ×1
express ×1
java ×1
linq ×1
long-polling ×1
maven ×1
mocha.js ×1
mongodb ×1
node.js ×1
oop ×1
overloading ×1
sizeof ×1
struct ×1