Baruch
Baruch

Reputation: 21498

Property fall-through in C# user control

I am trying to make a user control that will consist of a text-box and "browse" button, which will open a file dialog and update the text box with the selection (like the HTML file upload box). While I want the selected file property to be mapped to the text box, I want almost all other properties of the file dialog to be publicly available from my control (filter, etc.)

My 2 questions:

  1. For properties of my control that are directly mapped to a property of one of the inner controls, is there some shortcut (either in the language or in the VS IDE) to do this easily without manually writing the get/set pair and adding the single line to each to return/set the corresponding property?
  2. Is there some way (language or IDE shortcut) to do the above for multiple properties? Basically I want my control to have all the properties of the file dialog.

Upvotes: 2

Views: 263

Answers (2)

Buh Buh
Buh Buh

Reputation: 7546

This sounds like a job for T4!

You could use T4 and reflection to create a partial class with all these properties in. If its only this one class then it might become more work than doing it manually; I guess you would need to decide how often you would want to use it.

EDIT:

T4 means: Text Template Transformation Toolkit

It is built into Visual Studio and alows you to write templates which will generate your code for you.
It can be useful any time you need to write any sort of repetative or boilerplate code.

I have made a start for you. Create a new file in your solution called MyUserControl.tt and copy the code below.
As soon as you save, a new file called MyUserControl.cs will appear and list all your properties.

As this makes a partial class, you will want to tweek the class name and namespace to match that of your existing user control. I have assumed your textbox's name is Target, but you can obviously tweek that too.

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Web" #>
<#@ import namespace="System.Reflection" #> 

public partial class MyUserControl
{
<#
PropertyInfo[] properties = typeof(System.Web.UI.WebControls.TextBox).GetProperties(
    BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo property in properties)
{
    WriteLine(string.Format("    public {0} {1}", property.PropertyType.FullName, property.Name));
    WriteLine("    {");

    if(property.GetGetMethod() != null)
    {
        WriteLine("        get { return Target." + property.Name + "; } ");
    }

    if(property.GetSetMethod() != null)
    {
        WriteLine("        set { Target." + property.Name + " = value; } ");
    }

    WriteLine("    }");
    WriteLine("");
}

#>
}

Upvotes: 3

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174369

You can do this using ReSharper.
Assuming the following class:

public class YourUserControl
{
    private OpenFileDialog _openFileDialog;
    private TextBox _textBox;
}

You can put the cursor inside the class and hit Alt+Ins and choose "Delegating members". This will give you a list of all public members of both fields. You can select the ones you need and off you are:

enter image description here

Upvotes: 1

Related Questions