views:

434

answers:

5

Hi,

I'm not quite sure how to do that, but what I would like to do is to create a special type of property that will perform specific tasks at the get and set, and will be defined on generic type. For example, when writing this:


    MyProp<String> name;

a pre-defined get and set will be performed on the string value.

How can that be done?

Thanks!

A: 

You would need to create a generic class named MyProp. Then, you will need to add implicit or explicit cast operators so you can get and set the value as if it were the type specified in the generic type parameter. These cast operators can do the extra work that you need.

John Fisher
would not work I think with only the cast operators, one also would need the assignment operator which is not overloadable...
Tim Mahy
@Tim: Cast operators go both ways. When assigning a string to a MyProp<string>, if there is an implicit cast operator, it would get used.
John Fisher
yes but a developer would still be able to set the property with a MyProp<string> value without having it being intercepted....
Tim Mahy
@Tim: Sure, but that's not the usage model he wanted. Using it in the expected, straightforward, simple, normal way would not have this issue.
John Fisher
+1  A: 

You just declare the property the normal way using a generic type:

public MyType<string> PropertyName { get; set; }

If you want to call predefined methods to do something in the get or set, implement the property getter/setter to call those methods.

dthorpe
You can also declare properties with unbound generic types, which makes things more fun: public MyType<T> PropertyName { get; set; }. If you implement the getter/setter, the type of the value will be T and will vary with how it is used.
dthorpe
+3  A: 

Okay, I'll bite. You want something like this: If you declare a "property" like this:

Update: I'm now pretty sure that Fredrik Mörk answered your question and gave a solution. I'm not really happy with the idea, but it seems to answer exactly what I understood from your question.

public class PropertyFoo {
  public MyProp<String> Name;
}

this ends up as

public class PropertyFoo {
  public string Name {
    get { /* do predefined stuff here */ }
    set { /*other predefined stuff here */ }
  }
}

No. Not possible and not a property, really. Look for template/snippet support in your IDE.

Benjamin Podszun
+1 the most logical conclusion.
leppie
+1  A: 
public class MyProp<T>
{
...
}

public class ClassThatUsesMyProp
{
public MyProp<String> SomeProperty { get; set; }
}
ileon
+4  A: 

You can make a generic class like this:

public class MyProp<T>
{
    private T _value;

    public T Value
    {
        get
        {
            // insert desired logic here
            return _value;
        }
        set
        {
            // insert desired logic here
            _value = value;
        }
    }

    public static implicit operator T(MyProp<T> value)
    {
        return value.Value;
    }

    public static implicit operator MyProp<T>(T value)
    {
        return new MyProp<T> { Value = value };
    }
}

...then use it in a class like so:

class SomeClass
{
    public MyProp<int> SomeProperty { get; set; }
}

The implicit operators means that you do not need to explicitly set or get the Value property of MyProp, but can write code to access the value in a more "natural" way:

SomeClass instance = new SomeClass();
instance.SomeProperty = 32;
int someInt = instance.SomeProperty;
Fredrik Mörk
I think this is missing to take "..to create a special type of property that will perform specific tasks at the get and set.." into account - but since this question is really unclear I might be wrong.
Benjamin Podszun
@Benjamin: yes, the question is a bit unclear, but that is where the `insert desired logic here` comments come into the picture in my code sample. You could put in some logic that will be performed whenever the getter or setter is invoked, regardless of which property it is done for.
Fredrik Mörk
Whoa, I completely missed to see the whole picture. I'm really no friend of implicit operators, but - I guess you answered the question just fine. +1.
Benjamin Podszun
Yep, this is exactly what I was looking for.Thanks!
rachmos
+1 for writing out what I had described.
John Fisher