它是内部修饰符我无法实例化XmlElement的原因吗?

时间:2016-12-09 11:36:57

标签: c# .net powershell

我正在尝试创建System.Xml.XmlElement的实例。我注意到,它的构造函数签名是:

protected internal XmlElement(
    string prefix,
    string localName,
    string namespaceURI,
    XmlDocument doc
)

AFAIK内部修饰符使构造函数仅在其程序集(System.Xml.dll)中可见。我试图在我的PowerShell脚本中实例化它。

我的代码:

$doc = new-object System.Xml.XmlDocument
$e = New-Object System.Xml.XmlElement -ArgumentList "foo","bar","baz",$doc
New-Object : A constructor was not found. Cannot find an appropriate constructor for type System.Xml.XmlElement.
At line:1 char:6
+ $e = New-Object System.Xml.XmlElement -ArgumentList "foo","bar","baz",$doc
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

另外,我实际上根本没有找到构造函数:

([type]"System.Xml.XmlElement").GetConstructors()

(产生0行输出)

受保护的关键字怎么样? 阻止我访问构造函数,对吗?

1 个答案:

答案 0 :(得分:1)

无法使用构造函数,因为其受保护的内部(仅适用于派生类)。但是,只需使用CreateElement方法:

$doc = new-object System.Xml.XmlDocument
$e = $doc.CreateElement("foo","bar","baz")