如何枚举c#中的文本框属性? [英] How to enumerate through properties of textbox in c#?

查看:88
本文介绍了如何枚举c#中的文本框属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何枚举Wpf中的文本框属性(使用C#)?



实际上我想通过复制与此克隆相同的所有属性来创建文本框的克隆除了文本框的 Id / Name 之外的文本框,以便它们都是不同的文本框。



我认为我可以通过使用它来实现相同的目的 XamlWriter 但我想检查是否还有其他合适的替代品。



我希望我的问题清楚而且重点突出。但如果它造成任何混淆,请告诉我。



谢谢。

How to enumerate through properties of textbox in Wpf (using C#)?

Actually I wanted to create a clone of textbox by copying all properties same to this cloned textbox except the Id/Name of the textbox so that they both would be different textboxes.

I think I can achieve the same by using XamlWriter but I wanted to check if there is any other suitable alternate.

I hope that my question is clear and to the point. But if it has created any confusions please let me know.

Thanks.

推荐答案

这可以是使用反射完成。但这不是一个矫枉过正的事吗?反思可以做得更多;当你没有预先存在的类型知识时,它可以通过类型元数据发现以类型无关的方式克隆对象,但是你正在讨论一种固定类型。此外,反射相对较慢。



那么,你确定你不想只是手动编码克隆吗?没有?然后向这个方向钻取:



首先,得到运行时类型 http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx [ ^ ]。



你得到了 System.Type 的实例。现在,获取属性: http://msdn.microsoft.com/en-us/library/kyaxdd3x。 aspx [ ^ ]。



对于每个属性,您可以通过 getter setter 访问属性值。您将需要一个类型实例,一个可以获取或设置属性值的引用:

http://msdn.microsoft.com/en-us/library/kyaxdd3x.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.canread.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system。 reflection.propertyinfo.canwrite.aspx [ ^ ],

http://msdn.microsoft.com/en-us/ library / system.reflection.propertyinfo.getmethod.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.setmethod.aspx [ ^ ]。



上面显示的最后两个链接解释了如何获取getter和setter。它的类型为 MethodInfo

http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.aspx [ ^ ]。



最后,使用getter或setter来获取或设置属性变量,使用调用方法:

http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/4k9x6bc0.aspx [ ^ ]。



基本上,就是我吨。您有源对象,它的引用,创建一个新对象,获取它的引用。使用两个引用读取所有属性,在另一个中设置,这两个引用在上面引用的两种调用方法之一中作为参数名为 obj 传递。







我刚才描述了最通用的方法,完全类型无关。您还可以考虑特定于类型的方法。请看下面的评论;由于我解释的原因,让我们假设浅层克隆。 (但是,我们可以考虑深度克隆,我实际尝试过;它在.NET中不可用,因为实际存在的东西。)



你需要派生所有类型你想克隆,如果他们没有实现 System.IClonable ,就实现它。请参阅:

http://msdn.microsoft.com/en -us / library / system.icloneable.aspx [ ^ ]。



也许这就是你已经知道的,然后我只是确认它的适用性。



-SA
This can be done using Reflection. But won't it be an overkill? Reflection can do a lot more; it can clone objects in type-agnostic manner, through type metadata discovery, when you have no preexisting knowledge of the type, but you are talking about one fixed type. Besides, reflection is relatively slow.

So, are you sure that you don't want just to code cloning manually? No? Then drill down in this direction:

First, get a runtime type: http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx[^].

You got the instance of System.Type. Now, get the properties: http://msdn.microsoft.com/en-us/library/kyaxdd3x.aspx[^].

With each property, you can access property values through its getter and setter. You will need a type instance, a reference for which you can get or set a property value:
http://msdn.microsoft.com/en-us/library/kyaxdd3x.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.canread.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.canwrite.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.getmethod.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.setmethod.aspx[^].

The last two links shown above explain how to obtain getter and setter. It has the type MethodInfo:
http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.aspx[^].

Finally, you use a getter or a setter to get or set the property variable, through using the invocation method:
http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx[^],
http://msdn.microsoft.com/en-us/library/4k9x6bc0.aspx[^].

Basically, that's it. You have source object, its reference, create a new object, obtain its reference. Read all properties in one, set in another one, using the two references, which are passed as the parameter named obj in one of two invocation methods referenced above.



I just described the most universal approach, fully type-agnostic. You can also consider type-specific approach. Please see my comment below; let's assume shallow cloning, by the reasons I explained. (However, we can consider deep cloning, which I actually tried out; it is not available in .NET as something actually existing.)

You need to derive all types you want to clone, and, if they not implement System.IClonable, implement it. Please see:
http://msdn.microsoft.com/en-us/library/system.icloneable.aspx[^].

Perhaps this is what you already know, then I'm just confirming its applicability.

—SA


这个克隆在WPF中看起来像个大问题?如果对象不可序列化,则无法使用深度克隆解决方案进行反序列化。



底部的解决方案按其属性克隆UI对象。

可悲的是,我无法让它复制事件处理程序。



This cloning looks like a big problem in WPF? If the object is not serializable you can't use the deepclone solution with deserialisation.

The solution at the bottom clones a UI object by its properties.
Sadly I could not get it to work to copy the event handlers also.

''' <summary>
''' Clone object, by copying all properties NOT THE EVENT HANDLERS
''' </summary>
''' <param name="o"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function CloneObjectByProperties(o As Object) As Object
    Try
        'get all properties if the type
        Dim t As Type = o.[GetType]()
        Dim properties As PropertyInfo() = t.GetProperties()
        'instantiate a new object for the found type
        Dim p As [Object] = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, Nothing, o, Nothing)
        'copy all properties
        For Each pi As PropertyInfo In properties
            If pi.CanWrite Then
                pi.SetValue(p, pi.GetValue(o, Nothing), Nothing)
            End If
        Next

        ''Copy handlers
        'Dim _copyHelper As New clsCopyEventHandlers()
        'Try
        '    _copyHelper.GetHandlersFrom(o).CopyTo(p)
        'Catch ex As Exception
        '    'just continue, could not copy events
        'End Try

        Return p
    Catch ex As Exception
        Throw New Exception("CloneObjectByProperties failed: " & ex.Message)
    End Try

End Function


这篇关于如何枚举c#中的文本框属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆