问题列表 - 第30643页

为什么CLR不总是调用值类型构造函数

我有一个关于Value类型中的类型构造函数的问题.这个问题的灵感来自Jeffrey Richter在CLR中通过C#3rd ed编写的内容,他说(在第195页 - 第8章)你不应该在值类型中实际定义类型构造函数,因为有时候CLR不会调用它.

所以,例如(好吧......实际上是Jeffrey Richters的例子),即使通过查看IL,我也无法解决为什么在以下代码中没有调用类型构造函数:

internal struct SomeValType
{
    static SomeValType()
    {
        Console.WriteLine("This never gets displayed");
    }
    public Int32 _x;
}
public sealed class Program
{
    static void Main(string[] args)
    {
        SomeValType[] a = new SomeValType[10];
        a[0]._x = 123;
        Console.WriteLine(a[0]._x);     //Displays 123
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,对类型构造函数应用以下规则,我只是看不出为什么根本没有调用上面的值类型构造函数.

  1. 我可以定义一个静态值类型构造函数来设置类型的初始状态.
  2. 一个类型只能有一个构造函数 - 没有默认构造函数.
  3. 类型构造函数是隐式私有的
  4. JIT编译器检查是否已在此AppDomain中执行了类型的类型构造函数.如果不是它将调用发送到本机代码,否则它不会,因为它知道该类型已经"初始化".

所以......我无法理解为什么我看不到这个类型的数组正在被构造.

我最好的猜测是它可能是:

  1. CLR构造类型数组的方式.我原以为在创建第一个项目时会调用静态构造函数
  2. 构造函数中的代码不会初始化任何静态字段,因此会被忽略.我已经尝试在构造函数中初始化私有静态字段,但该字段仍然是默认值0 - 因此不会调用构造函数.
  3. 或者......由于公共Int32被设置,编译器以某种方式优化了构造函数调用 - 但这是一个模糊的猜测!

最佳实践等帮助,我只是对它非常感兴趣,因为我希望能够亲眼看到它为什么不被调用.

编辑:我在下面添加了我自己的问题的答案,只是引用杰弗里里希特所说的.

如果有人有任何想法,那将是辉煌的.非常感谢,詹姆斯

c# clr struct static-constructor typeinitializer

37
推荐指数
2
解决办法
1660
查看次数

.Net二进制反序列化运行时平台的故障检测/取证

我正在寻找有关如何检测运行时平台以揭示Microsoft .Net二进制反序列化失败的源类型的见解.

When using BinaryFormatter.Deserialize(StreamingContextStates.CrossMachine) and one of the types does not exist in the current binaries; instead of throwing an error, .Net inserts the object [TypeLoadExceptionHolder]. Particularly for collections, this causes no immediate problem.

Subsequently when the collection is serialized for transmission between application tiers; the platform receives a 'serialization failure' because [TypeLoadExceptionHolder] cannot be serialized. So the resulting error is useless for actually providing clues as to the source-type that caused the problem. Now the hunt (time …

.net serialization instrumentation

12
推荐指数
1
解决办法
2583
查看次数

personal web site hacked on ovh.com, code added in html

My friend has a web site on ovh.com.Since a couple of days, the site is flagged as dangerous by google.

I had a look in the files (the site only contains only html, css, pjg) and it appears that a new line of code:

<script>http://...page.php</script></body>
Run Code Online (Sandbox Code Playgroud)

(I do not remember the exact url) has been added in some of the html pages. This is obviously a virus that would be run when the page is displayed.

If I delete this line …

html security virus web

3
推荐指数
1
解决办法
814
查看次数

iPhone(iOS):将文件从主包复制到文档文件夹错误

我试图在首次启动时将我的应用程序包中的一些文件复制到文档目录.我有第一次启动的检查,但为了清楚起见,它们未包含在代码段中.问题是我正在复制到文档目录(已经存在),在文档中,它说明:

在操作之前,dstPath不得存在.

对我来说直接复制到文档根目录的最佳方法是什么?我想这样做的原因是允许iTunes文件共享支持.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Populator"];

  NSLog(@"\nSource Path: %@\nDocuments Path: %@", sourcePath, documentsDirectory);

  NSError *error = nil;

  if([[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:documentsDirectory error:&error]){
    NSLog(@"Default file successfully copied over.");
  } else {
    NSLog(@"Error description-%@ \n", [error localizedDescription]);
    NSLog(@"Error reason-%@", [error localizedFailureReason]);
  }
  ...
  return YES;
}
Run Code Online (Sandbox Code Playgroud)

谢谢

iphone nsfilemanager

8
推荐指数
2
解决办法
2万
查看次数

将快捷键分配给WPF中的按钮

如何在WPF中为按钮指定快捷键?

谷歌搜索给了我答案,在标准的Winforms中追加_而不是'&'.

所以在我做完如下之后:

<Button Name="btnHelp" Content="_Help"></Button> 
Run Code Online (Sandbox Code Playgroud)

我没有发现'H'下划线.

这是第一个问题.

第二个问题是,如何在运行时按Alt + H后执行该操作.如果只是为了示例而显示一个消息框就足够了.

我正在使用C#,WPF

谢谢.

wpf c#-3.0

21
推荐指数
3
解决办法
5万
查看次数

Java Generics:无法将List <SubClass>强制转换为List <SuperClass>?

刚遇到这个问题:

List<DataNode> a1 = new ArrayList<DataNode>();
List<Tree> b1 = a1;  // compile error: incompatible type
Run Code Online (Sandbox Code Playgroud)

DataNode类型是Tree的子类型.

public class DataNode implements Tree
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,这适用于数组:

DataNode[] a2 = new DataNode[0];
Tree[] b2 = a2;   // this is okay
Run Code Online (Sandbox Code Playgroud)

这有点奇怪.谁能对此作出解释?

java generics

104
推荐指数
5
解决办法
5万
查看次数

在Flash Builder 4中使用Ant任务

有人可以帮我开始在Flash Builder 4中使用ant.我之前从未使用过ant,所以我是一个完整的新手.我首先需要知道如何安装它,以及如何完成基础知识.

任何帮助表示赞赏.

ant flash-builder flex4

5
推荐指数
1
解决办法
4336
查看次数

在Oracle WebLogic Server中定位外部服务器的常见做法是什么?

我们正试图想出一些接近简单直接模型的东西,用于在WebLogic中定位JMS资源(我知道这很有可能).队列和主题可以轻松而直观地映射到WebLogic服务器上运行的JMS服务器,但外部服务器及其中的资源似乎更棘手.

在WLS 10.0和10.3中,首先,外部服务器不是在JMS服务器旁边定义,而是作为JMS模块的成员定义.其次,默认情况下,它们定位到它们所定义的JMS模块的目标,即WLS集群或WLS服务器,而不是通过子部署针对JMS服务器的"非外部"资源.

但是,通过高级定位,还可以在JMS服务器上定位外部服务器.这导致模型相对于外来/"非外来"JMS资源更加对称.

高级定位http://dexter.xebialabs.com/Media/foreign_server_advanced_targeting.png

所以,问题是:

  1. 除了历史意外之外还有任何理由为什么外部资源和"非外国"资源定位是如此不同(默认情况下,WLS群集或WLS服务器上的外部资源与JMS服务器上的非外部资源相比)?
  2. 针对外国和非外国资源有任何共同或最佳做法吗?
  3. 是否有人不希望通过子部署在JMS服务器上定位外部服务器?

提前致谢!

安德鲁菲利普斯

java weblogic middleware

15
推荐指数
1
解决办法
2268
查看次数

在scala中匹配case类:〜(a,b)匹配{case a~b => ...}

我有一个案例课

case class ~[a,b](_1:a, _2:b)
Run Code Online (Sandbox Code Playgroud)

当我想做pattetn匹配

new ~("a", 25) match{
  case "a" ~ 25 =>
}
Run Code Online (Sandbox Code Playgroud)

我可以使用这种方式,因为"a" ~ 25~("a", 25)是等价的.但如果我想new ~("a", new ~("b", 25))通过{case "a" ~ "b" ~ 25 => }麻烦来匹配开始.我知道这些陈述并不等同.那么,如何 new ~("a", new ~("b", 25))呈现?按什么规则?

scala pattern-matching case-class

6
推荐指数
1
解决办法
468
查看次数

C++优化问题

我有一些正在使用boost库的中型项目,因此在调试应用程序性能方面受到了影响(Visual Studio 2008).

我现在使用的解决方案意味着即使在调试模式下打开函数内联,这也带来了足够的性能,但肯定会有一些缺点.

如果我强制使用函数inlining(/Ob2)开关,有谁知道在调试功能方面我会失去什么?

也许有人对加速boost /其他模板库的调试性能有任何其他想法?

c++ debugging optimization boost

3
推荐指数
1
解决办法
215
查看次数