How to save a link to a generic type created from an expression passed to a method?

I have the following method that returns a generic type object INamedProperty<TReturn>based on the return type of a specific expression. I need to save a reference to an object that is returned by this method for further processing. What type should I store? Will it be Objectokay? How do I get it back to the corresponding INamedProperty<TReturn>later? Do I also need to save the type TReturn?

public class PropertyBuilder<T> : IPropertyBuilder<T> where T : class {
    public INamedProperty<TReturn> Named<TReturn>(Expression<Func<T, TReturn>> property) {
        o = new NamedProperty<TReturn>();
        // how do I store o as an instance of the encapsulating class?
    }
}

Thanks in advance!

+3
source share
2 answers

I would try to implement a generic, uhh, non- generic INamedPropertyone that could perform the necessary operations:

interface INamedProperty
{
    // Informational
    Type ContainingType { get; }
    string Name { get; }
    Type ReturnType { get; }

    // Operations (for example)
    void CopyTo(object obj, INamedProperty property);
}

Then implement them in a generic NamedProperty:

class NamedProperty<T> : INamedProperty { ... }
+2

Action ? , TReturn ; .

:

public INamedProperty<TReturn> Named<TReturn>(Expression<Func<T, TReturn>> property, Action<INamedProperty<TReturn>> action)
{
    var o = new NamedProperty<TReturn>();
    action.Invoke(o);
    return o;
}
0

All Articles