views:

212

answers:

2

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
+2  A: 

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.

Jon Skeet
i updated my question
marc.d
Yes, but it still talks about "reference types" and "by ref" without really differentiating.
Jon Skeet
+2  A: 

With MVC the Expression approach isn't uncommon; as such, here's some example code that shows how to pass an Expression into such a function, and get the value and metadata (attributes); the Expression<Func<object>> (or similar) replaces the ref argument:

(sorry, example is C# - my VB isn't strong enough to try)

static void Main()
{
    Person p = new Person { FirstName = "abc" };
    MyMethod(() => p.FirstName);
}
public static void MyMethod(Expression<Func<object>> expression)
{
    object value = expression.Compile()();
    Console.WriteLine("value is: " + value);
    switch (expression.Body.NodeType)
    {
        case ExpressionType.MemberAccess:
            var me = (MemberExpression)expression.Body;
            AutotabAttribute attrib = (AutotabAttribute)
                Attribute.GetCustomAttribute(
                    me.Member, typeof(AutotabAttribute));
            if (attrib != null)
            {
                Console.WriteLine("maxlength is: " + attrib.maxlength);
                Console.WriteLine("text is: " + attrib.text);
            }
            break;                
        default:
            throw new NotSupportedException("Expression is too complex");
    }
}

So you would have to write an overload of TextBoxAutoTab that takes the Expression... instead of the ByRef, does the evaluation (as above) and writes the html appropriately.

Marc Gravell
thx i try it this way now.
marc.d