Search code examples
.netreference-type

get attribute of a property in .NET after passing the property to a function as ref


is it possible to pass a property eg. Person.Firstname by reference to a function and then still be able to read the attributes of Person.Firstname through this reference type?

so does the reference type know that it is not only a string but also the Firstname property of class Person?

tia


i try to write a extension for my asp.net mvc app that uses jquery.autotab and adds the necessary js code to the Html.TextBox output based on a attribute.

for example

Class Person
  <Autotab("text", maxlength:= 15)> _
  Property Firstname() as String
  ...
  End Property
End Class


<%= Html.TextBoxAutoTab("Person.Firstname", p.Firstname) %>

the signature of TextBoxAutoTab looks like this

  Public Function TextBoxAutoTab(ByVal h As HtmlHelper, ByVal name As String, ByRef value As Object) As String

Solution

  • Leaving the whole reference type vs "as ref" issue aside (see my comment), no - there's no way of telling where a value came from within a method which received it as a parameter.

    You could pass in an expression tree instead (in .NET 3.5) and then compile/execute the tree to get the value, and examine the tree to work out what it means.

    However, this is a design smell really. Why do you need to know this? What are you trying to achieve?

    EDIT: Note that when you pass a property by reference in VB (you can't do it in C#) it really just copies the current value to a local variable, passes that local variable by reference, and then copies the new value of the local variable back to the property. The code that's been called has no indication that it was originally from a property.