MAF(System.AddIn)属性,它是一个集合

时间:2012-11-02 19:56:15

标签: .net c#-4.0 maf system.addin

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

合同大会

[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;
    }
}

1 个答案:

答案 0 :(得分:1)

通过遵循this MSDN演练中讨论的示例,我能够自己解决这个问题。基本上,我只在上面的代码示例中有一半的等式。在AddView适配器和HostView适配器中,您需要从视图转换为合同,从视图转换为给定报表参数的合同。我已经更新了问题中的代码示例,以便在他们有这种需要的情况下,希望它能在某个时间内饶过其他人。