3

In application setting of visual c#, we can create a series of settings with specific Name, Type, Scope and Value. I have access to the value by the code:

string color= Myproject.Properties.Settings.Default.mycolor;

How can I get "mycolor", which is the name of this setting, in the output?

Moj
  • 31
  • 5
  • 1
    Can you clarify whether you want to retrieve the property name from the setting value (e.g. "blue") or using the setting accessor ("mycolor" property)? – Cᴏʀʏ Sep 17 '14 at 21:10
  • I wanted to get "mycolor" and not the value. I resolved it by creating a dictionary with the key "mycolor" so I could find the key by value. I had series of these settings so I found dictionary the best for my application. I am fairly new to C#, so there might be some other ways to do this. – Moj Sep 23 '14 at 19:08

3 Answers3

3

If you want the name of the setting, then you are looking to get the property name. In the book Metaprogramming in .NET, Kevin Hazzard has a routine that looks something like this:

/// <summary>
/// Gets a property name string from a lambda expression to avoid the need
/// to hard-code the property name in tests.
/// </summary>
public static string GetPropertyName<T>(Expression<Func<T>> expression)
{
    MemberExpression body = (MemberExpression)expression.Body;
    return body.Member.Name;
}

To call it you would do this:

string propertyName = GetPropertyName(() => Myproject.Properties.Settings.Default.mycolor);

I've added a static reflection utility to some of my projects to allow for access to this and other tools.

EDIT

With July 20, 2015 being set as the RTM date for Visual Studio 2015 and .NET 4.6, this seems like a good time for an update.

Happily, all of my above code goes away in C# 6 (.NET 4.6) since there is a new nameof expression that takes care of this very easily now:

string propertyName = nameof(Myproject.Properties.Settings.Default.mycolor);

Some of the new features are described on the MSDN blog.

JoelC
  • 3,664
  • 9
  • 33
  • 38
  • Has has only the value, so the string color, he seems not to have access to `Settings.Default.mycolor`. Otherwise the question would be pretty pointless. – Tim Schmelter Sep 17 '14 at 20:58
2

Here is my understanding of your requirement:

  • You need to know the setting-name from an object
  • f.e. you want to get "mycolor" from a color like "Red"(presuming that this is the default-value)

You can use the Properties collection and Enumerable.FirstOrDefault:

var colorProperty = Settings.Default.Properties.Cast<System.Configuration.SettingsProperty>()
    .FirstOrDefault(p => color.Equals(p.DefaultValue)); // color f.e "Red"
string nameOfProperty = null;
if (colorProperty != null)
    nameOfProperty = colorProperty.Name;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

A little extension method will help you out here:

public static string GetSettingName<TObject, TProperty>(this TObject settings, 
    Expression<Func<TObject, TProperty>> member) 
    where TObject : System.Configuration.ApplicationSettingsBase
{
    var expression = (MemberExpression)member.Body;
    return expression.Member.Name;
}

And it's usage:

var settingName = Properties.Settings.Default.GetSettingName(s => s.mycolor);
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • If i understand the question OP has only the color, for example "red" and he wants to know the relevant setting-name. It would be pretty pointless to get `mycolor` if he already needs to use `s.mycolor`. – Tim Schmelter Sep 17 '14 at 21:02
  • @TimSchmelter: OP states *I have access to the value by the code* (see example) and asks *How can I get "mycolor", which is the name of this setting*. I'm still reading that as "give me the setting property name as a string [using reflection]." I do agree that it's useless, OP could just type "mycolor" somewhere. – Cᴏʀʏ Sep 17 '14 at 21:05
  • He has access to the _value_ which is the color. However, maybe i've misunderstood it. – Tim Schmelter Sep 17 '14 at 21:06
  • From your answer I see where you're coming from. You're translating the setting value back to the setting name, whereas I interpreted it as given the property accessor, give me the property name. I have asked OP to clarify. – Cᴏʀʏ Sep 17 '14 at 21:09