CS0122和CS0143之间的编译错误差异

时间:2021-06-16 09:48:23

Let's consider the following first example:

让我们考虑下面的第一个例子:

public class ClassNameExample
{
    private ClassNameExample()
    {
        //code goes here    
    }
}

Now, if I try to instantiate the ClassNameExample class from within the same assembly, I'll get an "Inaccessible due to it's protection level" compiler error message (CS0122).

现在,如果我尝试从同一个程序集中实例化ClassNameExample类,我将得到一个“由于它的保护级别而无法访问”的编译器错误消息(CS0122)。

However, if I try to instantiate the ClassNameExample class from a different assembly, I'm getting a "The type 'class' has no constructors defined" compiler error message (CS0143)

但是,如果我尝试从另一个程序集中实例化ClassNameExample类,就会得到一个“类型'类'没有定义构造函数”的编译器错误消息(CS0143)

Can someone explain why the compiler sees them different?

有人能解释为什么编译器看到它们不同吗?

For reference, I've tried this in Visual Studio 2012, .NET 4.5.

作为参考,我在Visual Studio 2012,。net 4.5中尝试过。

1 个解决方案

#1


1  

I tried to see what Microsoft.CSharp.CSharpCodeGenerator does and to figure out where the errors are populated, but the code at the end relies on external files.

我想看看微软。CSharpCodeGenerator就是这样做的,它能找出错误的填充位置,但最后的代码依赖于外部文件。

The only answer I can give you is: if you create an instance with

我能给你的唯一答案是:如果你用它创建一个实例

Assembly.GetExecutingAssembly().CreateInstance("ClassNameExample");

or even with Activator.CreateInstance("Your assembly", "ClassNameExample");

甚至与活化剂。调用CreateInstance(“组装”、“ClassNameExample”);

both return null because they rely on public constructor, check the flags

它们都返回null,因为它们依赖于公共构造函数,请检查标志

    public object CreateInstance(string typeName)
    {
      return this.CreateInstance(typeName, false, BindingFlags.Instance | BindingFlags.Public, (Binder) null, (object[]) null, (CultureInfo) null, (object[]) null);
    }

    Activator.CreateInstance(assemblyName, typeName, false, BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, (Binder) null, (object[]) null, (CultureInfo) null, (object[]) null, (Evidence) null, ref stackMark);

#1


1  

I tried to see what Microsoft.CSharp.CSharpCodeGenerator does and to figure out where the errors are populated, but the code at the end relies on external files.

我想看看微软。CSharpCodeGenerator就是这样做的,它能找出错误的填充位置,但最后的代码依赖于外部文件。

The only answer I can give you is: if you create an instance with

我能给你的唯一答案是:如果你用它创建一个实例

Assembly.GetExecutingAssembly().CreateInstance("ClassNameExample");

or even with Activator.CreateInstance("Your assembly", "ClassNameExample");

甚至与活化剂。调用CreateInstance(“组装”、“ClassNameExample”);

both return null because they rely on public constructor, check the flags

它们都返回null,因为它们依赖于公共构造函数,请检查标志

    public object CreateInstance(string typeName)
    {
      return this.CreateInstance(typeName, false, BindingFlags.Instance | BindingFlags.Public, (Binder) null, (object[]) null, (CultureInfo) null, (object[]) null);
    }

    Activator.CreateInstance(assemblyName, typeName, false, BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, (Binder) null, (object[]) null, (CultureInfo) null, (object[]) null, (Evidence) null, ref stackMark);