隐藏公共功能 [英] Hiding Public Functions
问题描述
这是一个相当笼统的问题,因为我只是想知道有哪些可能的选择.
This is a fairly generic question in that I'm just wondering what potential options there are for this.
比如说,我有一个简单的类:
Say, I have a simple class:
Public Class Example
Private _One As String
Public Property One() As String
Get
Return _One
End Get
Set(ByVal value As String)
_One = value
End Set
End Property
Public Function DoSomething() As Integer
End Function
End Class
上面的类只是可以从 Web 服务返回的示例.我根本无权修改它,抱歉,如果我没有说清楚.
是否有可能以某种方式克隆此类,使其保留所有属性值,但隐藏存在公共函数的事实?
Is it possible to somehow make a clone this class, so that it retains all of the properties values, but hides the fact that there is a Public function?
我希望能够采用我们从 Web 服务(我们没有编写)检索到的一些现有类,并能够将它们传递给应用程序以使用,但不公开函数.我不想沿着创建自己的类的路线走下去,这些类专门定义每个属性并将值写入其中(由于其中一些的绝对大小),所以我想看看是否有任何动态我可以利用(也许有一种使用反射的方法?).
I'd like to be able to take some existing classes we retrieve from a web service (which we didn't write) and be able to pass them on for use in an application, but without exposing the functions. I don't want to go down the route of creating my own classes that specifically define each property and write the values in (due to the sheer size of some of them), so I'm looking to see if there is anything dynamic I can utilise (maybe there is a way using reflection?).
任何想法将不胜感激.
谢谢,标记
推荐答案
看起来下面的文章概述了一些您可能会觉得有用的技巧.http://coding-passion.blogspot.com/2008/12/adding-properties-to-object-dynamically.html
Looks like the following article outlines some techniques that you may find useful. http://coding-passion.blogspot.com/2008/12/adding-properties-to-object-dynamically.html
作者正在向对象动态添加属性,这基本上是您想要做的.您将遇到的唯一问题"是,因为属性是动态的,您将需要使用反射来获取和设置它们(您的应用程序在运行之前不会意识到这些属性 - 将无法在设计时直接引用它们).下面是一些实现此目的的示例方法.
The author is dynamically adding properties to an object which is essentially what you're going to want to do. The only "problem" that you will run in to would be, because the properties are dynamic, you will need to use reflection to get and set them (your app will not be aware of the properties until it runs - won't be able to directly reference them at design time). Below are some sample methods to do that.
除此之外,我不知道从类继承时隐藏"公共方法的方法.
Beyond that, I'm not aware of a way to "hide" public methods when inheriting from a class.
Public Function SetProperty(ByVal obj As Object, ByVal PropertyName As String, ByVal val As Object) As Boolean
Dim property_value As Object
Dim properties_info As System.Reflection.PropertyInfo() = obj.GetType.GetProperties
Dim property_info As System.Reflection.PropertyInfo
For Each prop As System.Reflection.PropertyInfo In properties_info
If prop.Name = PropertyName Then property_info = prop
Next
If property_info IsNot Nothing Then
Try
property_info.SetValue(obj, val, Nothing)
Return True
Catch ex As Exception
Return False
End Try
Else
Return False
End If
End Function
Public Function GetProperty(ByVal obj As Object, ByVal PropertyName As String) As Object
Dim property_value As Object
Dim properties_info As System.Reflection.PropertyInfo() = obj.GetType.GetProperties
Dim property_info As System.Reflection.PropertyInfo
For Each prop As System.Reflection.PropertyInfo In properties_info
If prop.Name = PropertyName Then property_info = prop
Next
If property_info IsNot Nothing Then
Try
property_value = property_info.GetValue(obj, Nothing)
Return property_value
Catch ex As Exception
Return Nothing
End Try
Else
Return Nothing
End If
End Function
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.setvalue.aspxhttp://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.getvalue.aspx
这篇关于隐藏公共功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!