Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Pass a property by reference

Tags:

Is there anyway to pass the property of an Object by reference? I know I can pass the whole object but I want to specify a property of the object to set and check it's type so I know how to parse. Should I maybe take another approach (I cannot change the original object in anyway)?

public class Foo{     public Foo(){}     public int Age { get; set; } }  private void setFromQueryString(object aProperty, String queryString, HttpContext context) {     //here I want to handle pulling the values out of      //the query string and parsing them or setting them     //to null or empty string...     String valueString = context.Request.QueryString[queryString].ToString();       //I need to check the type of the property that I am setting.      //this is null so I can't check it's type     Type t = aProperty.GetType(); }  private void callingMethod(HttpContext context) {     Foo myFoo = new Foo();     setFromQueryString(myFoo.Age, "inputAge", context); } 
like image 739
ctrlShiftBryan Avatar asked Mar 03 '10 21:03

ctrlShiftBryan


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language w3schools?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

You can call the function with a lambda expression:

private void setFromQueryString<T>(Action<T> setter, String queryString, HttpContext context)  {      //here I want to handle pulling the values out of       //the query string and parsing them or setting them      //to null or empty string...      String valueString = context.Request.QueryString[queryString].ToString();        //I need to check the type of the property that I am setting.       //this is null so I can't check it's type      Type t = typeof(T);      ...     setter(value); }  

You would call it like this:

setFromQueryString<int>(i => myFoo.Age = i, "inputAge", context); 

EDIT: If you really want type inference:

private void setFromQueryString<T>(Func<T> getter, Action<T> setter, String queryString, HttpContext context) {     ... } setFromQueryString(() => myFoo.Age, i => myFoo.Age = i, "inputAge", context); 
like image 87
SLaks Avatar answered Oct 27 '22 02:10

SLaks


As others have pointed out, you can do this using delegates, using one of the many ways to specify delegates. However, if you intend to do this regularly, you should consider creating a wrapper type for passing properties by reference that wraps the delegates required, it may create a nicer API.

For example:

class PropertyReference<T> {    public T Value    {        get        {            return this.getter();        }         set        {            this.setter(value);        }    }     public PropertyReference(Func<T> getter, Action<T> setter)    {       this.getter = getter;       this.setter = setter;    } } 

That way you can pass around a reference to your property and modify it by setting the reference value.

var reference = new PropertyReference(                         () => this.MyValue,                         x => this.MyValue = x);  reference.Value = someNewValue; 
like image 41
Jeff Yates Avatar answered Oct 27 '22 04:10

Jeff Yates