我知道在.NET中,所有数组都派生自System.Array并且System.Array类实现IList,ICollection和IEnumerable.实际的数组类型也实现IList<T>,ICollection<T>和IEnumerable<T>.
这意味着如果你有一个String[],那么那个String[]对象也是a System.Collections.IList和a System.Collections.Generic.IList<String>;.
不难看出为什么那些IList会被认为是"ReadOnly",但令人惊讶的是......
String[] array = new String[0];
Console.WriteLine(((IList<String>)array).IsReadOnly); // True
Console.WriteLine(((IList)array).IsReadOnly); // False!
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,尝试通过Remove()和RemoveAt()方法删除项会导致NotSupportedException.这表明两个表达式都对应于ReadOnly列表,但IList的ReadOnly属性不返回预期值.
怎么会?
当我运行此代码时,应用程序以ClassNotFoundException退出:
//uncaught ClassNotFoundException
try
{
Class<?> clazz = defineClass(null, bytes, 0, bytes.length, null);
table.put(clazz.getName(), clazz);
}
catch (NoClassDefFoundError e)
{
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译此代码时,编译器会抱怨ClassNotFoundException无法访问,因为它不会从try-catch语句的try-clause中抛出.
//Won't compile
try
{
Class<?> clazz = defineClass(null, bytes, 0, bytes.length, null);
table.put(clazz.getName(), clazz);
}
catch (ClassNotFoundException e)
{
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,捕获的唯一throwable是NoClassDefFoundError.
//catches throwable of type java.lang.NoClassDefFoundError,
//with a java.lang.ClassNotFoundException as its cause
try
{
Class<?> clazz = defineClass(null, bytes, 0, bytes.length, null);
table.put(clazz.getName(), clazz);
}
catch (Throwable e)
{
System.out.println(e.getClass().getName());
System.out.println(e.getCause().getClass().getName());
}
Run Code Online (Sandbox Code Playgroud)
下面的代码将编译并捕获错误(并且只有错误),但它很笨拙:
//possible workaround
try
{
Class<?> …Run Code Online (Sandbox Code Playgroud) 这给了我一个Document对象,其顶级节点没有子节点:
public static Document getDocument(Object jaxb)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().newDocument();
JAXBContext context = JAXBContext.newInstance(jaxb.getClass());
context.createMarshaller().marshal(jaxb, doc);
return doc;
}
Run Code Online (Sandbox Code Playgroud)
这是解决方法,它看起来效率更低,因为它转换为String然后转换为Document.
public static Document getDocument(Object jaxb)
{
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(jaxb.getClass());
context.createMarshaller().marshal(jaxb, writer);
return DocumentBuilderFactory.newInstance().newDocumentBuilder().
parse(new InputSource(new StringReader(writer.toString()));
}
Run Code Online (Sandbox Code Playgroud)
有可能完成我想要完成的任务吗?
我读过的关于这个主题的大多数答案都指向System.Windows.Forms.WebBrowser类或来自Microsoft HTML Object Library程序集的COM接口mshtml.HTMLDocument.
WebBrowser类没有引导我到任何地方.以下代码无法检索由我的Web浏览器呈现的HTML代码:
[STAThread]
public static void Main()
{
WebBrowser wb = new WebBrowser();
wb.Navigate("https://www.google.com/#q=where+am+i");
wb.DocumentCompleted += delegate(object sender, WebBrowserDocumentCompletedEventArgs e)
{
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb.Document.DomDocument;
foreach (IHTMLElement element in doc.all)
{
System.Diagnostics.Debug.WriteLine(element.outerHTML);
}
};
Form f = new Form();
f.Controls.Add(wb);
Application.Run(f);
}
Run Code Online (Sandbox Code Playgroud)
以上只是一个例子.我真的不想找到一个解决方法来找出我所在城镇的名称.我只需要了解如何以编程方式检索那种动态生成的数据.
(调用新的System.Net.WebClient.DownloadString(" https://www.google.com/#q=where+am+i "),将生成的文本保存到某处,搜索您当前所在城镇的名称找到了,如果你能找到它,请告诉我.)
但是,当我从我的网络浏览器(即或Firefox)访问" https://www.google.com/#q=where+am+i "时,我会在网页上看到我的城镇名称.在Firefox中,如果我右键单击城镇名称并选择"Inspect Element(Q)",我会清楚地看到用HTML代码编写的城镇名称,这看起来与WebClient返回的原始HTML完全不同.
在我厌倦了玩System.Net.WebBrowser后,我决定给mshtml.HTMLDocument一个镜头,最后得到同样无用的原始HTML:
public static void Main()
{
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)new mshtml.HTMLDocument();
doc.write(new System.Net.WebClient().DownloadString("https://www.google.com/#q=where+am+i"));
foreach (IHTMLElement e in doc.all)
{
System.Diagnostics.Debug.WriteLine(e.outerHTML);
}
}
Run Code Online (Sandbox Code Playgroud)
我想必须有一种优雅的方式来获取这种信息.现在,我能想到的是将一个WebBrowser控件添加到表单中,让它导航到相关的URL,发送键"CLRL,A",并将页面上显示的任何内容复制到剪贴板并尝试解析它.不过,这是一个可怕的解决方案.
而不是右键单击宏标识符然后"转到定义",是否可以扩展宏引用并查看传递给编译器的代码是什么样的?
我认为字符集的名称是"Unicode",而"UTF-8"是Unicode字符集的特定编码的名称,但我经常看到术语"编码"和"字符集"在引用时可互换使用到UTF-8.
例如,
<meta charset="UTF-8">
Run Code Online (Sandbox Code Playgroud)
VS
<?xml version="1.0" encoding="UTF-8" ?>
Run Code Online (Sandbox Code Playgroud) 这使用Eclipse编译find:
abstract class CollectionView implements Collection<Object> {
...
public Object[] toArray(Object[] o) {
if (fast) {
return get(map).toArray(o);
} else {
synchronized (map) {
return get(map).toArray(o);
}
}
}
...
}
class KeySet extends CollectionView implements Set<Object> {
protected Collection<Object> get(Map<Object, Object> map) {
return map.keySet();
}
protected Object iteratorNext(Map.Entry entry) {
return entry.getKey();
}
}
Run Code Online (Sandbox Code Playgroud)
但是使用Ant时无法编译:
错误:KeySet不是抽象的,并且不会覆盖Set中的抽象方法toArray(T [])
我可以看到代码使用Eclipse编译的原因:KeySet已经从CollectionView继承了toArray(T [])方法的实现.
但是当我使用Ant编译时为什么会失败?
<javac srcdir="src" destdir="bin" debug="on">
<compilerarg value="-Xlint:unchecked"/>
<compilerarg value="-Xlint:deprecation"/>
</javac>
Run Code Online (Sandbox Code Playgroud) 判断COM对象的引用计数是否达到0的一种方法是尝试访问其中一个成员并捕获生成的InvalidComObjectException,这不是很优雅,并且似乎不能很好地适应.另一种方法是调用Marshal.ReleaseComObject并检查结果,但这需要将Com对象的引用计数减少1.
有简单明了的方法吗?
当我声明一个在引用的Visual C++程序集中定义的Visual C++类型的变量时,我不会被引发的BadImageFormatException所吸引,因为我对这个异常未被catch子句捕获这一事实感到好奇.紧接着变量声明的try-catch语句,但是try-catch语句的catch子句围绕方法调用声明变量的方法.
public static void method()
{
try
{
Some_Visual_Cpp_Type o;
}
catch (Exception)
{
Console.WriteLine("caught inside the method");//apparently not called
}
}
public static void Main()
{
try
{
method();
}
catch (BadImageFormatException)
{
Console.WriteLine("caught outside the method");//prints "caught outside the method"
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这种行为吗?
我们可以在.NET中解决这个问题:
interface I<A> {}
interface I<A, B> {}
Run Code Online (Sandbox Code Playgroud)
...但在Java中,相同的代码将导致编译错误.
这很有趣,因为即使类型信息在运行时消失,人们也会期望有关类型参数数量的信息仍然存在.
如果此限制与类型擦除有关,有人可以解释原因吗?
java ×4
c# ×3
.net ×2
exception ×2
generics ×2
visual-c++ ×2
ant ×1
arrays ×1
c++ ×1
character ×1
com ×1
com-interop ×1
compilation ×1
dom ×1
dynamic-html ×1
eclipse ×1
encoding ×1
html ×1
javascript ×1
jaxb ×1
macros ×1
marshalling ×1
preprocessor ×1
rcw ×1
throwable ×1
type-erasure ×1
unicode ×1
utf-8 ×1
xml ×1