如何在继承的 ControlDesigner 类中获取 ITypeDescriptorContext 和 IServiceProvider

How to obtain ITypeDescriptorContext and IServiceProvider in inherited ControlDesigner class(如何在继承的 ControlDesigner 类中获取 ITypeDescriptorContext 和 IServiceProvider)
本文介绍了如何在继承的 ControlDesigner 类中获取 ITypeDescriptorContext 和 IServiceProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 ControlDesigner 类继承到 MyControlDesigner.

I have inherited ControlDesigner class into MyControlDesigner.

在这个类中,我需要获取 ITypeDescriptorContextIServiceProvider 接口后面的对象,但我不知道如何:(

In this class I need to obtain objects behind ITypeDescriptorContext and IServiceProvider interfaces, but I don't know how :(

我需要这两个接口在方法中传递它们,但我在任何其他对象中都找不到它们.

I need these 2 interfaces to pass them in method, but I can't find them inside any other object.

谁能帮帮我.

谢谢

最好的问候博扬

推荐答案

System.Design程序集中有一个内部类EditorServiceContext实现了ITypeDescriptorContextIServiceProviderIWindowsFormsEditorService.

There is an internal class EditorServiceContext in System.Design assembly which has implemented ITypeDescriptorContext, IServiceProvider and IWindowsFormsEditorService.

您可以找到它并创建该类的实例或使用其静态方法来满足您的要求.例如,要显示属性的编辑器,您可以这样使用它:

You can find it and create an instance of the class or use its static methods to satisfy your requirement. For example to show the editor for a property, you can use it this way:

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" });

<小时>

如果出于某种原因您需要类的源代码来对其进行一些更改,这里是该类的代码:


If for any reason you need the source of the class to apply some changes to it, here is the code for that class:

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

这篇关于如何在继承的 ControlDesigner 类中获取 ITypeDescriptorContext 和 IServiceProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)