首页 > 解决方案 > 构造函数重载隐藏 PowerShell 中的属性

问题描述

我有一个有两个不同构造函数的类,它有 4 个属性。出于我们的目的,它看起来类似于以下内容:

class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    public string Publisher { get; set; }
    public int PubDate { get; set; }
 
    public Book(string title, string author)
    {
        Title = title;
        Author = author;
    }
 
    public Book(string title, string author, string publisher, int pubDate)
    {
        Title = title;
        Author = author;
        Publisher = publisher;
        PubDate = pubDate;
    }
}

当用户只传入两个参数时,第一个构造函数在技术上应该创建对象。

我遇到的问题是,我想在 PowerShell 中使用用于实例化它的任何参数来显示该对象,例如,仅使用 Title 和 Author 实例化的 Book,应仅在终端中显示 Book 的 Title 和 Author。

我正在研究使用format.ps1xml来格式化它。

标签: c#powershellformattingconstructor-overloading

解决方案


推荐阅读