2009-11-18 92 views
0

這是一個相當普遍的問題,我只是想知道這有什麼潛在的選擇。隱藏公共職能

說,我有一個簡單的類:

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服務返回的例子。我根本無法修改它,如果我沒有說清楚,很抱歉。

是否有可能以某種方式製作一個克隆這個類,以便它保留所有的屬性值,但隱藏的事實,有一個公共職能?

我希望能夠從Web服務中獲取一些現有的類(我們沒有寫),並且能夠將它們傳遞給應用程序使用,但不公開函數。我不想沿着創建我自己的專門定義每個屬性並寫入值的類的路徑(由於其中一些屬性的絕對大小),所以我期待着看看是否有任何動態I可以利用(也許有使用反射的方法?)。

任何想法將不勝感激。

謝謝, 馬克

回答

1

看起來像下面的文章概述了一些技術,你可能會覺得有用。 http://coding-passion.blogspot.com/2008/12/adding-properties-to-object-dynamically.html

作者正在動態地將屬性添加到一個對象中,這實質上就是您要做的事情。你將運行的唯一「問題」是,因爲屬性是動態的,你將需要使用反射來獲取和設置它們(你的應用程序在運行之前不會意識到屬性 - 不能在設計時直接引用它們)。以下是一些示例方法。

除此之外,我還沒有意識到從類中繼承「隱藏」公共方法的方法。


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.aspx http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.getvalue.aspx

+0

謝謝,這看起來可能有用。而不是「隱藏」功能,如果我可以將屬性複製到實現相同目標的新對象。我會研究這個並回復你,謝謝。 – ca8msm 2009-11-18 12:45:14

0

如果你只是想揭露庫中的方法(和您的應用程序隱藏),我認爲你想要的朋友關鍵字,而不是在你的方法Public關鍵字。

+0

您好,我無法改變的功能的訪問級別,因爲他們在我們沒有寫編譯Web服務。所以,我想能夠克隆這個類並以某種方式隱藏這些函數。 – ca8msm 2009-11-18 09:50:40

+0

我完全錯過了那一點,對不起。我會有一個想法,但我不會抱有希望。 – adrianos 2009-11-18 09:55:23

0

如果你不能夠改變web服務(如果可以的話,你可以從DoSomething的功能刪除[WebMethod]),也許你可以創建一個從客戶方web服務代理派生類中,通過使用public new integer DoSomething() {}

隱藏方法

(對不起,我不知道VB.net的語法)

如果你想使它適用於任何對象,你可以設備某種外觀,完全隱藏你的對象。 門面有公共財產public Object obj { get; set; }和兩個函數ReadMember和SetMember。這兩個函數通過反射來獲取和設置obj上的屬性。

+0

嗨,謝謝你的回答,但這並不能使解決方案變得動態。我正在尋找一些可以帶上_any_類的東西(我發佈的這個東西只是一個示例),並且隱藏了公共職能。 – ca8msm 2009-11-18 09:44:26

0

這個類是一個通用對象,從在到PropertyBag中的構造函數傳遞的對象提取屬性。然後可以通過屬性名稱通過泛型類型屬性檢索屬性值。

Imports System 
Imports System.Collections.Generic 

Public Class PropertyBag 

    Private _Ints As Dictionary(Of String, Integer) 
    Private _Strings As Dictionary(Of String, String) 

    Sub New(ByVal obj As Object) 

     Dim name As String 
     Dim value As Object 
     Dim prop As Reflection.PropertyInfo 

     _Ints = New Dictionary(Of String, Integer) 
     _Strings = New Dictionary(Of String, String) 

     For Each prop In obj.GetType.GetProperties 
      name = prop.Name.ToUpper 
      value = prop.GetValue(obj, Nothing) 

      Select Case prop.PropertyType.Name 
       Case "Int32" 
        _Ints.Add(name, CType(value, Integer)) 
       Case "String" 
        _Strings.Add(name, value.ToString) 
      End Select 
     Next 

    End Sub 

    Public Function [Integer](ByVal sKey As String) As Integer 
     Return _Ints(sKey.ToUpper) 
    End Function 
    Public Function [String](ByVal sKey As String) As String 
     Return _Strings(sKey.ToUpper) 
    End Function 

End Class 

樣本用法是...

Public Class Example 

    Private _one As String 
    Private _two As Integer 

    Public Property One() As String 
     Get 
      Return _one 
     End Get 
     Set(ByVal value As String) 
      _one = value 
     End Set 
    End Property 
    Public Property Two() As Integer 
     Get 
      Return _two 
     End Get 
     Set(ByVal value As Integer) 
      _two = value 
     End Set 
    End Property 

    Public Function DoSomething() As Integer 
     Return 666 
    End Function 

End Class 

Sub Main() 

    Dim s As String 
    Dim i As Integer 
    Dim ex As New Example 

    ex.One = "ONE" 
    ex.Two = 2 

    Dim o As New PropertyBag(ex) 

    s = o.String("One") 
    i = o.Integer("Two") 

    Stop 

End Sub