Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a generic property

People also ask

Which is a generic property?

In topology and algebraic geometry, a generic property is one that holds on a dense open set, or more generally on a residual set, with the dual concept being a nowhere dense set, or more generally a meagre set.

How do you declare a generic property in C#?

Generic means the general form, not specific. In C#, generic means not specific to a particular data type. C# allows you to define generic classes, interfaces, abstract classes, fields, methods, static methods, properties, events, delegates, and operators using the type parameter and without the specific data type.

What does generic mean in C#?

Generics refer to a feature in C# that allows defining a class or method with type as a parameter. Generics allow for designing a classes and methods whose types are specified only at the time of declaration and instantiation.

How do you define generic method?

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.


It's possible if the class containing the property is generic, and you declare the property using the generic parameter:

class Foo<TValue> {
    public string Value { get; set; }
    public TValue TypedValue {
        get {
            return (TValue)Convert.ChangeType(Value, typeof(TValue));
        }
    }
}

An alternative would be to use a generic method instead:

class Foo {
    public string Value { get; set; }
    public Type TheType { get; set; }

    public T CastValue<T>() {
         return (T)Convert.ChangeType(Value, typeof(T));
    }
}

You can also use the System.ComponentModel.TypeConverter classes to convert, since they allow a class to define it's own converter.

Edit: note that when calling the generic method, you must specify the generic type parameter, since the compiler has no way to infer it:

Foo foo = new Foo();
foo.Value = "100";
foo.Type = typeof(int);

int c = foo.CastValue<int>();

You have to know the type at compile time. If you don't know the type at compile time then you must be storing it in an object, in which case you can add the following property to the Foo class:

public object ConvertedValue {
    get {
        return Convert.ChangeType(Value, Type);
    }
}

Properties, events, constructors etc can't be generic - only methods and types can be generic. Most of the time that's not a problem, but I agree that sometimes it's a pain. Brannon's answer gives two reasonable workarounds.


I don't believe the example you've given here is possible. The type of CastedValue has to be defined at compile time, which means it can't depend on a runtime value (the value of the TheType property).

EDIT: Brannon's solution has some good ideas for how to handle this using a generic function rather than a property.