2017-09-26 69 views
0

我在同一個項目ProjectView和FeatureView中有兩個類。我需要從另一個班級訪問一個班級的BindingSource。我有一個kluge,我使BindingSource範圍internal,而不是private。恥辱,恥辱。有一個更好的方法嗎?。從另一個類訪問一個類中的BindingSource

// ProjectView.cs 
public partial class ProjectView : System.Windows.Forms.UserControl { 

} 

// ProjectView.Designer.cs 
partial class ProjectView { 
    // This should be private 
    internal System.Windows.Forms.BindingSource bsFeatures; 
} 

// FeatureView.cs 
public partial class FeatureView : System.Windows.Forms.UserControl { 

    // Get ProjectView 
    Project currentProject = this._presenter.WorkItem.State["CurrentProject"] as Infrastructure.Interface.Aml.BusinessEntities.Project; 
    string key = System.String.Concat("Project", currentProject.Id); 
    this._presenter.WorkItem.State["CurrentProject"] = currentProject; 
    ProjectView view = _presenter.WorkItem.Items.Get<ProjectView>(key); 

    // Populate currentProject.Features with ProjectView.bsFeatures.List 
    currentProject.Features.Clear(); 
    IList featureList = view.bsFeatures.List; 
    foreach (Feature feature in featureList) 
    { 
     currentProject.Features.Add(feature); 
    } 

} 

回答

2

也許類似的東西,不知道:

partial class ProjectView 
{ 
    // This should be private 
    private System.Windows.Forms.BindingSource bsFeatures; 

    public System.Windows.Forms.BindingSource BindingSource 
    { 
     get { return bsFeatures; } 
    } 

    public void ShareOnlyWith(FeatureView fw) 
    { 
     fw.BindingSource = bsFeatures; 
    } 
} 
當然,我們打破的原則之一

,不依賴於具體的東西。