Is it possible to "extend" the property "class"?

I'm new to C # (started last week), so be cool with me;). I would like to know if I can somehow write my own property, let me explain:

I have some partial classes that I populate by adding properties, but the pattern of all getters and setters is the same, so I would like to factorize this:

public partial class Travel { public String TravelName { get { return LocaleHelper.GetRessource(Ressource1); } set { if (this.Ressource1 == null) Ressource1 = new Ressource() { DefaultValue = value }; else Ressource1.DefaultValue = value; } } public String TravelDescription { get { return LocaleHelper.GetRessource(Ressource2); } set { if (this.Ressource2 == null) Ressource2 = new Ressource() { DefaultValue = value }; else Ressource2.DefaultValue = value; } } } 

As you can see, the only thing that has changed is Ressource1 / Ressource2. My goal is to write something like:

 public partial class Travel { public LocalizedString TravelName(Ressource1); public LocalizedString TravelDescription(Ressource2); } 

Anyone have an idea to do this, or another idea to make my code cleaner? Thanks,

Guillaume

+4
source share
7 answers

There is no way to do this in C # or .NET, but if you are doing a lot of them, it might be worth exploring aspect-oriented programming through postsharp . This basically allows you to define an attribute that causes additional code to be added at compile time. The code you enter looks something like this:

 public partial class Travel { [LocalizedProperty(source = "Ressource1") public string TravelName { get; set; } [LocalizedProperty(source = "Ressource2") public string TravelDescription{ get; set; } } 

And at compile time, PostSharp will replace the property with the template that you defined in the new LocalizedPropertyAttribute class.

+7
source

You cannot make it as brief as what you describe, but you can reduce the complexity and redundancy of setters.

 private void SetRessource(ref Ressource res, string value) { if(res == null) res = new Ressource(); res.DefaultValue = value; } public String TravelName { get { return LocaleHelper.GetRessource(Ressource1); } set { SetRessource(ref this.Ressource1, value); } } public String TravelDescription { get { return LocaleHelper.GetRessource(Ressource2); } set { SetRessource(ref this.Ressource2, value); } } 
+3
source

I don’t know exactly what you are trying to achieve, but you can make things too complicated. Wouldn't that be enough?

 public class Travel { /// <summary> /// Creates a new instance of <see cref="Travel"/>. /// </summary> public Travel() { this.TravelName = Resources.DefaultTravelName; this.TravelDescription = Resources.DefaultTravelDescription; } public string TravelName { get; set; } public string TravelDescription { get; set; } } 

where Resources is the generated class (from the resx file) for localized resources. I have the feeling that you are trying to create your own localization, because you still do not know that .NET already has the infrastructure for this .

+1
source

No, there is no such way. This would be possible in php , but not in C #.

In this case, you should change your approach from properties.

UPD: You could probably use something similar for each property (with the exception of obvious weakness):

 public class Prop { Resource _res; public Prop(Resource res) { this._res = res; } public string Value { get { return LocaleHelper.GetRessource(_res); } set { if(_res == null) // This is a weak point as it now // as it wont work else _res.DefaultValue = value; } } 
0
source

You can implement one indexed property by providing you with one of the following two syntax options based on your preferences. The code will basically be a function that takes a specially named resource and returns the correct content.

 Travel t = new Travel(); string x = t["Name"]; or string x = t[Travel.Name]; 
0
source

You can make your life easier by encapsulating the getter and setter logic in a base class, and then simply calling these methods any new properties you create (just acting as a thin shell around these methods). Here is an example:

 public class Travel : LocalizedRessourceSubscriber { private Ressource<string> Ressource1 = null; private Ressource<string> Ressource2 = null; public String TravelName { get { return GetRessource<string>(Ressource2); } set { SetRessource<string>(Ressource1, value); } } public String TravelDescription { get { return GetRessource<string>(Ressource2); } set { SetRessource<string>(Ressource2, value); } } } public class LocalizedRessourceSubscriber { protected T GetRessource<T>(Ressource<T> Source) { return LocaleHelper.GetRessource<T>(Source); } protected void SetRessource<T>(Ressource<T> Source, T Value) { (Source ?? (Source = new Ressource<T>()) ).DefaultValue = Value; } } 

... Thus, there is very little logic in your properties, and you repeat less code. This suggests the following classes (which I made fun of as generalized):

 public static class LocaleHelper { public static T GetRessource<T>(Ressource<T> Source) { return default(T); } } public class Ressource<T> { public T DefaultValue { get; set; } } 
0
source

It does not make sense. using the properties as you currently have them, you can simply write:

  Travel t = new Travel(); string tvlName = t.TravelName; string desc = t.TravelDescription; 

If you changed the way you want, you will also need to specify a parameter

  Travel t = new Travel(); LocalizedString tvlName = t.TravelName([someresopurcedesignator]); LocalizedString desc = t.TravelDescription([someresopurcedesignator]); 

all you could do was create a propertyBag emulator

  public class Travel { private LocalizedString props = new LocalizedString(); public LocalizedString Propertys { get { return props; } set { props = value; } } } public class LocalizedString // this is property Bag emulator { public string this[string resourceName] { get{ return LocaleHelper.GetRessource(resourceName); } set{ LocaleHelper.GetRessource(resourceName) = value; } } } 

You would access this as follows:

  Travel t = new Travel(); t.Propertys[NameResource1] = "Bob Smith"; t.Propertys[DescriptionResource2] = "Fun trip to discover the orient"; string tvlName = t.Propertys[NameResource1]; string desc = t.Propertys[DescriptionResource2]; 
-1
source

All Articles