2011-01-26 2 views
5

ControlDesignerクラスをMyControlDesignerに継承しました。継承したControlDesignerクラスのITypeDescriptorContextおよびIServiceProviderの入手方法

私は ITypeDescriptorContextIServiceProviderインターフェースの後ろにオブジェクトを取得する必要がありますが、私はこの方法でそれらを渡すために、これらの2つのインターフェイスが必要ですが、私は内側にそれらを見つけることができませんどのように:(

知りません。このクラスで

他のオブジェクト。

誰かが私を助けてください。

敬具 ボヤン

ありがとう
+0

それは古い質問だと、おそらくあなたはもう答えは必要ありませんが、将来の読者は[私の答え]かもしれません(http://stackoverflow.com/questions/4811446/how-to-以下のような便利な機能を提供しています:#43827054) –

答えて

1

Component.SiteはIServiceProvider、ITypeDescriptorContextについてIDKは、私はあなたが行うことができますIServiceProvider取得するには...あまりにもそれを

を探しています実装します。var SP =(IServiceProvider)Component.Siteを。

+0

あなたの答えをありがとう、それは動作します。 ITypeDescriptorContextの検索を続けます。見つけたらここに書きます。再度、感謝します。 – Bojan

+0

喜んで助けてくれるでしょう:) – VoidMain

+0

これは古い質問ですが、もう答えは必要ないかもしれませんが、将来の読者は[私の答え]を見つけるかもしれません(http://stackoverflow.com/questions/4811446/how-to-以下のような便利な機能を提供しています:#43827054) –

2

ITypeDescriptorContextIServiceProviderIWindowsFormsEditorServiceを実施している内部クラスEditorServiceContextSystem.Designにおけるアセンブリがあります。

クラスのインスタンスを作成したり、静的メソッドを使用して要件を満たすことができます。あなたはそれにいくつかの変更を適用するには、クラスのソースを必要とする何らかの理由で、ここでのコードがある場合は

var editorServiceContext = typeof(ControlDesigner).Assembly.GetTypes() 
    .Where(x => x.Name == "EditorServiceContext").FirstOrDefault(); 
var editValue = editorServiceContext.GetMethod("EditValue", 
    System.Reflection.BindingFlags.Static | 
    System.Reflection.BindingFlags.Public); 
editValue.Invoke(null, new object[] { this, this.Component, "The Property Name" }); 

:プロパティのエディタを表示するたとえば、あなたはこのようにそれを使用することができますそのクラス:

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.ComponentModel.Design; 
using System.Drawing.Design; 
using System.Windows.Forms; 
using System.Windows.Forms.Design; 

internal class EditorServiceContext : 
    IWindowsFormsEditorService, ITypeDescriptorContext, IServiceProvider 
{ 
    // Fields 
    private IComponentChangeService _componentChangeSvc; 
    private ComponentDesigner _designer; 
    private PropertyDescriptor _targetProperty; 

    // Methods 
    internal EditorServiceContext(ComponentDesigner designer) 
    { 
     this._designer = designer; 
    } 

    internal EditorServiceContext(ComponentDesigner designer, PropertyDescriptor prop) 
    { 
     this._designer = designer; 
     this._targetProperty = prop; 
     if (prop == null) 
     { 
      prop = TypeDescriptor.GetDefaultProperty(designer.Component); 
      if ((prop != null) && typeof(ICollection).IsAssignableFrom(prop.PropertyType)) 
      { 
       this._targetProperty = prop; 
      } 
     } 
    } 

    internal EditorServiceContext(ComponentDesigner designer, PropertyDescriptor prop, string newVerbText) : this(designer, prop) 
    { 
     this._designer.Verbs.Add(new DesignerVerb(newVerbText, new EventHandler(this.OnEditItems))); 
    } 

    public static object EditValue(ComponentDesigner designer, object objectToChange, string propName) 
    { 
     PropertyDescriptor prop = TypeDescriptor.GetProperties(objectToChange)[propName]; 
     EditorServiceContext context = new EditorServiceContext(designer, prop); 
     object obj2 = prop.GetValue(objectToChange); 
     object obj3 = (prop.GetEditor(typeof(UITypeEditor)) as UITypeEditor).EditValue(context, context, obj2); 
     if (obj3 != obj2) 
     { 
      try 
      { 
       prop.SetValue(objectToChange, obj3); 
      } 
      catch (CheckoutException) 
      { 
      } 
     } 
     return obj3; 
    } 

    private void OnEditItems(object sender, EventArgs e) 
    { 
     object component = this._targetProperty.GetValue(this._designer.Component); 
     if (component != null) 
     { 
      CollectionEditor editor = TypeDescriptor.GetEditor(component, typeof(UITypeEditor)) as CollectionEditor; 
      if (editor != null) 
      { 
       editor.EditValue(this, this, component); 
      } 
     } 
    } 

    void ITypeDescriptorContext.OnComponentChanged() 
    { 
     this.ChangeService.OnComponentChanged(this._designer.Component, this._targetProperty, null, null); 
    } 

    bool ITypeDescriptorContext.OnComponentChanging() 
    { 
     try 
     { 
      this.ChangeService.OnComponentChanging(this._designer.Component, this._targetProperty); 
     } 
     catch (CheckoutException exception1) 
     { 
      if (exception1 != CheckoutException.Canceled) 
      { 
       throw; 
      } 
      return false; 
     } 
     return true; 
    } 

    object IServiceProvider.GetService(Type serviceType) 
    { 
     if ((serviceType == typeof(ITypeDescriptorContext)) || (serviceType == typeof(IWindowsFormsEditorService))) 
     { 
      return this; 
     } 
     if ((this._designer.Component != null) && (this._designer.Component.Site != null)) 
     { 
      return this._designer.Component.Site.GetService(serviceType); 
     } 
     return null; 
    } 

    void IWindowsFormsEditorService.CloseDropDown() 
    { 
    } 

    void IWindowsFormsEditorService.DropDownControl(Control control) 
    { 
    } 

    DialogResult IWindowsFormsEditorService.ShowDialog(Form dialog) 
    { 
     IUIService service = (IUIService)((IServiceProvider)this).GetService(typeof(IUIService)); 
     if (service != null) 
     { 
      return service.ShowDialog(dialog); 
     } 
     return dialog.ShowDialog(this._designer.Component as IWin32Window); 
    } 

    // Properties 
    private IComponentChangeService ChangeService 
    { 
     get 
     { 
      if (this._componentChangeSvc == null) 
      { 
       this._componentChangeSvc = (IComponentChangeService)((IServiceProvider)this).GetService(typeof(IComponentChangeService)); 
      } 
      return this._componentChangeSvc; 
     } 
    } 

    IContainer ITypeDescriptorContext.Container 
    { 
     get 
     { 
      if (this._designer.Component.Site != null) 
      { 
       return this._designer.Component.Site.Container; 
      } 
      return null; 
     } 
    } 

    object ITypeDescriptorContext.Instance 
    { 
     get 
     { 
      return this._designer.Component; 
     } 
    } 

    PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor 
    { 
     get 
     { 
      return this._targetProperty; 
     } 
    } 
} 
+0

私は[この例]のクラスをテストしました(http://stackoverflow.com/a/43826903/3110834)。また、私は[this one](http://stackoverflow.com/a/36794920/3110834)と[this one](http://stackoverflow.com/a/36314635/3110834)のライター版を実装しました。 –