1

我正在尝试使用 .NET4 System.AddIn 命名空间编写 MAF 插件。AddIn 有一个属性,它是一个名为“ReportParameters”的自定义类型的集合;AddIn 上还有其他属性,它们是 mscorlib 中包含的本机类型(即:整数、布尔值、字符串等),我从下面的代码片段中省略了这些属性,因为它们工作正常。我正在努力使用将 ReportParameters IListContract 转换为 IList 的 Host 和 AddIn 端适配器,反之亦然。任何帮助,将不胜感激。我的管道当前定义如下(参见 Host Adapter 中的注释):

合同组装

[AddInContract]
public interface IReportComponentContract: IContract
{
    IListContract<IReportParameterContract> ReportParameters { get; set; }
    bool GenerateReport();
}

public interface IReportParameterContract: IContract
{
    string Name { get; set; }
    string Value { get; set; }
}

插件视图程序集

[AddInBase]
public interface IReportComponent
{
    IList<IReportParameter> ReportParameters { get; set;}
    bool GenerateReport();
}

public interface IReportParameter
{
    string Name { get; set; }
    string Value { get; set; }
}

public class ReportParameter : IReportParameter
{
    public string Name
    {
        get;
        set;
    }

    public string Value
    {
        get;
        set;
    }
}

AddIn 主机适配器组件

[AddInAdapter]
public class ReportComponentViewToContractAddInAdapter: ContractBase, IReportComponentContract
{
    IReportComponent_view;

    public ReportComponentViewToContractAddInAdapter(IReportComponent view)
    { 
        _view = view;
    }

    public IListContract<IReportParameterContract> ReportParameters
    {
        get
        {
            return CollectionAdapters.ToIListContract<IReportParameter, IReportParameterContract>(_view.ReportParameters, ReportParameterAddInAdapter.ViewToContractAdapter, ReportParameterAddInAdapter.ContractToViewAdapter); 
        }
        set
        {
            _view.ReportParameters = CollectionAdapters.ToIList<IReportParameterContract, IReportParameter>(value, ReportParameterAddInAdapter.ContractToViewAdapter, ReportParameterAddInAdapter.ViewToContractAdapter); 
        }
    }

    public bool GenerateReport()
    {
        return _view.GenerateReport();
    }

    internal IReportComponent GetSourceView()
    {
        return _view;
    }
}

public class ReportParameterAddInAdapter
{
    internal static IReportParameter ContractToViewAdapter(IReportParameterContract contract)
    {
        if (!System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain(contract) &&
            (contract.GetType().Equals(typeof(ReportParameterViewToContractAddInAdapter))))
        {
            return ((ReportParameterViewToContractAddInAdapter)(contract)).GetSourceView();
        }
        else
        {
            return new ReportParameterContractToViewAddInAdapter(contract);
        }
    }

    internal static IReportParameterContract ViewToContractAdapter(IReportParameter view)
    {
        if (!System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain(view) &&
            (view.GetType().Equals(typeof(ReportParameterContractToViewAddInAdapter))))
        {
            return ((ReportParameterContractToViewAddInAdapter)(view)).GetSourceContract();
        }
        else
        {
            return new ReportParameterViewToContractAddInAdapter(view);
        }
    }
}

public class ReportParameterContractToViewAddInAdapter : IReportParameter
{
    private IReportParameterContract _contract;
    private ContractHandle _handle;

    public ReportParameterContractToViewAddInAdapter(IReportParameterContract contract)
    {
        _contract = contract;
        _handle = new ContractHandle(contract);
    }

    public string Name 
    { 
        get { return _contract.Name; }
        set { _contract.Name = value; }
    }

    public string Value 
    { 
        get { return _contract.Value;}
        set { _contract.Value = value; } 
    }

    internal IReportParameterContract GetSourceContract()
    {
        return _contract;
    }
}

public class ReportParameterViewToContractAddInAdapter : ContractBase, IReportParameterContract
{
    IReportParameter _view;

    public ReportParameterViewToContractAddInAdapter(IReportParameter view)
    {
        _view = view;
    }

    public virtual string Name
    { 
        get { return _view.Name; }
        set { _view.Name = value; }
    }

    public virtual string Value
    { 
        get { return _view.Value; }
        set { _view.Name = value; }
    }

    internal IReportParameter GetSourceView()
    {
        return _view;
    }
}

主机视图程序集

public interface IReportComponent
{
    IList<IReportParameter> ReportParameters { get; set; }
    bool GenerateReport();
}

public interface IReportParameter
{
    string Name { get; set; }
    string Value { get; set; }
}

public class ReportParameter : IReportParameter
{
    public string Name
    {
        get;
        set;
    }

    public string Value
    {
        get;
        set;
    }
}

主机适配器组件

[HostAdapter]
public class ReportComponentContractToViewHostAdapter: IReportComponent
{
    IReportComponentContract _contract;
    ContractHandle _lifetime;

    public ReportComponentContractToViewHostAdapter(IReportComponent contract)
    {
        _contract = contract;
        _lifetime = new ContractHandle(contract);
    }

    public IList<IReportParameter> ReportParameters
    {
        get 
        {
            return CollectionAdapters.ToIList<IReportParameterContract, IReportParameter>(_contract.ReportParameters, ReportParameterHostAdapter.ContractToViewAdapter, ReportParameterHostAdapter.ViewToContractAdapter); 
        }
        set 
        {
            _contract.ReportParameters = CollectionAdapters.ToIListContract<IReportParameter, IReportParameterContract>(value, ReportParameterHostAdapter.ViewToContractAdapter, ReportParameterHostAdapter.ContractToViewAdapter); 
        }
    }

    public  bool GenerateReport()
    {
        return _contract.GenerateReport();
    }

}

internal class ReportParameterContractToViewHostAdapter : IReportParameter
{
    private ContractHandle _handle;
    private IReportParameterContract _contract;

    public ReportParameterContractToViewHostAdapter(IReportParameterContract contract)
    {
        _contract = contract;
        _handle = new ContractHandle(contract);
    }

    public string Name
    {
        get { return _contract.Name; }
        set { _contract.Name = value; }
    }

    public string Value
    {
        get { return _contract.Value; }
        set { _contract.Value = value; }
    }

    internal IReportParameterContract GetSourceContract()
    {
        return _contract;
    }
}

public class ReportParameterHostAdapter
{
    internal static IReportParameter ContractToViewAdapter(IReportParameterContract contract)
    {
        if (!System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain(contract) &&
            (contract.GetType().Equals(typeof(ReportParameterViewToContractHostAdapter))))
        {
            return ((ReportParameterViewToContractHostAdapter)(contract)).GetSourceView();
        }
        else
        {
            return new ReportParameterContractToViewHostAdapter(contract);
        }
    }

    internal static IReportParameterContract ViewToContractAdapter(IReportParameter view)
    {
        if (!System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain(view) &&
            (view.GetType().Equals(typeof(ReportParameterContractToViewHostAdapter))))
        {
            return ((ReportParameterContractToViewHostAdapter)(view)).GetSourceContract();
        }
        else
        {
            return new ReportParameterViewToContractHostAdapter(view);
        }

    }
}

public class ReportParameterViewToContractHostAdapter: ContractBase, IReportParameterContract
{
    private IReportParameter _view;

    public ReportParameterViewToContractHostAdapter(IReportParameter view)
    {
        _view = view;
    }

    public virtual string Name
    {
        get { return _view.Name; }
        set { _view.Name = value; }
    }

    public virtual string Value
    {
        get { return _view.Value; }
        set { _view.Value = value; }
    }

    internal IReportParameter GetSourceView()
    {
        return _view;
    }
}
4

1 回答 1

1

按照这个MSDN 演练中讨论的示例,我能够自己解决这个问题。基本上,我在上面的代码示例中只有一半等式。在 AddView 适配器和 HostView 适配器中,您需要针对给定的报告参数执行从视图到合同的转换以及从视图到合同的转换。我已经更新了问题中的代码示例,希望它可以在其他人有这种需要时节省一些时间。

于 2012-11-05T19:41:58.113 回答