How to deal with GetDataPresent to let it accept all the derived types(如何处理 GetDataPresent 让它接受所有派生类型)
问题描述
我正在使用 drgevent.Data.GetDataPresent 来确定是否拖动的组件是否可以接受.
I'm using drgevent.Data.GetDataPresent to determine whether the dragged component is acceptable or not.
我有一个问题,我想接受一个特定的类型,比如 SomeType 以及从它派生的所有类型.似乎 GetDataPresent
不支持这样的要求.
I've got a problem which is that I want to accept a specific type say SomeType and all the types that derived from it. Seems GetDataPresent
doesn't support such requirement.
有什么想法吗?
推荐答案
只是不要使用 GetDataPresent(),它是样板文件,但您可以按照自己的方式进行操作.实际检索对象并检查您是否对它的类型感到满意:
Just don't use GetDataPresent(), it is boilerplate but you're free to do it your way. Actually retrieve the object and check if you're happy with its type:
protected override void OnDragEnter(DragEventArgs drgevent) {
var obj = drgevent.Data.GetData(drgevent.Data.GetFormats()[0]);
if (typeof(Base).IsAssignableFrom(obj.GetType())) {
drgevent.Effect = DragDropEffects.Copy;
}
}
Base 是基类的名称.虽然 GetFormats() 的使用看起来很奇怪,但这种方法可以保证有效,因为拖动 .NET 对象只会产生 one 格式,即对象类型的显示名称.这也是 GetDataPresent 不能用于派生对象的原因.
Where Base is the name of the base class. While the use of GetFormats() looks odd, this approach is guaranteed to work because dragging a .NET object only ever produces one format, the display name of the type of the object. Which is also the reason that GetDataPresent can't work for derived objects.
这篇关于如何处理 GetDataPresent 让它接受所有派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何处理 GetDataPresent 让它接受所有派生类型


基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01