我的问题可以通过这个片段总结出来:
public interface TheClass<T> {
public void theMethod(T obj);
}
public class A {
private TheClass<?> instance;
public A(TheClass<?> instance) {
this.instance = instance;
}
public void doWork(Object target) {
instance.theMethod(target); // Won't compile!
// However, I know that the target can be passed to the
// method safely because its type matches.
}
}
Run Code Online (Sandbox Code Playgroud)
我的类A
使用TheClass
其泛型类型未知的实例.它具有传递目标的方法,Object
因为TheClass
实例可以使用任何类进行参数化.但是,编译器不允许我像这样传递目标,这是正常的.
我该怎么做才能绕过这个问题?
一个肮脏的解决方案是将实例声明为TheClass<? super Object>
,它工作正常但在语义错误...
我之前使用的另一个解决方案是将实例声明为原始类型TheClass
,但这是不好的做法,所以我想纠正我的错误.
解
public class A {
private TheClass<Object> …
Run Code Online (Sandbox Code Playgroud) 我试图在输入字符串中找到每个"a - > b,c,d"模式.我使用的模式如下:
"^[ \t]*(\\w+)[ \t]*->[ \t]*(\\w+)((?:,[ \t]*\\w+)*)$"
Run Code Online (Sandbox Code Playgroud)
这种模式是一个C#模式,"\ t"指的是一个制表(它是一个单独的逃逸的litteral,由.NET String API解释),"\ w"指的是众所周知的正则表达式的预定义类,双重转义为由.NET STring API解释为"\ w",然后由.NET Regex API解释为"WORD CLASS".
输入是:
a -> b
b -> c
c -> d
Run Code Online (Sandbox Code Playgroud)
功能是:
private void ParseAndBuildGraph(String input) {
MatchCollection mc = Regex.Matches(input, "^[ \t]*(\\w+)[ \t]*->[ \t]*(\\w+)((?:,[ \t]*\\w+)*)$", RegexOptions.Multiline);
foreach (Match m in mc) {
Debug.WriteLine(m.Value);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
c -> d
Run Code Online (Sandbox Code Playgroud)
实际上,以"$"特殊字符结尾的行存在问题.如果我在"$"之前插入"\ r",它可以工作,但我认为"$"将匹配任何行终止(使用Multiline选项),尤其是Windows环境中的\ r \n.不是这样吗?
我需要检测Windows Phone 8设备上的摇动,以便启动特定操作.我怎样才能做到这一点 ?
我发现有几个提到微软为WinPhone7制作的"ShakeGesture.dll"库,但它唯一可用的地方(AppHub)被开发中心网站所取代......
谢谢你的帮助!
我正在使用Visual C#2010 Express,构建托管应用程序.
在"发布"模式下,Visual Studio生成一个程序集(dll或exe)以及一个包含程序集方法注释的XML文件.
在"调试"模式下,它生成程序集和PDB文件(包含调试信息).
由于我在调试模式下构建所有内容,我想知道如何使用程序集和pdb生成XML文件.为什么不生成?奇怪的行为,我们总是需要评论......
我的意见就像"Hi {{username}}"
,即.包含要替换的关键字的字符串.但是,输入非常小(大约10个关键字和1000个字符),并且我有一百万个可能的关键字存储在哈希表数据结构中,每个关联都与其替换相关联.
因此,我不想迭代关键字列表并尝试替换输入中的每一个,这是出于明显的性能原因.我更喜欢通过查找正则表达式模式在输入字符上只迭代一次"\{\{.+?\}\}"
.
在Java中,我使用Matcher.appendReplacement
和Matcher.appendTail
方法来做到这一点.但我找不到类似的API NSRegularExpression
.
private String replaceKeywords(String input)
{
Matcher m = Pattern.compile("\\{\\{(.+?)\\}\\}").matcher(input);
StringBuffer sb = new StringBuffer();
while (m.find())
{
String replacement = getReplacement(m.group(1));
m.appendReplacement(sb, replacement);
}
m.appendTail(sb);
return sb.toString();
}
Run Code Online (Sandbox Code Playgroud)
我自己被迫实施这样的API,还是我错过了什么?
此代码按预期打印"undefined":
console.log(foo());
function foo() {
return typeof a;
}
Run Code Online (Sandbox Code Playgroud)
这个崩溃时出现"a is not defined"错误:
const a = foo();
function foo() {
return typeof a;
}
Run Code Online (Sandbox Code Playgroud)
这是V8的错误还是预期的行为?
c# ×3
regex ×2
.net ×1
debugging ×1
generics ×1
gesture ×1
ios ×1
java ×1
javascript ×1
objective-c ×1