Get property from a shared object in C #

look at this code please:

public void BindElements<T>(IEnumerable<T> dataObjects)
{
    Paragraph para = new Paragraph();

    foreach (T item in dataObjects)
    {
        InlineUIContainer uiContainer =
            this.CreateElementContainer(item.FirstName ????? )              
        para.Inlines.Add(uiContainer);
    }                         

    FlowDocument flowDoc = new FlowDocument(para);
    this.Document = flowDoc;
}

When you write "item.XXX" in Visual Studio, I have to get properties from my entitiy, for example .FirstName or .LastName. I do not know how dataObjects is IEnumerable or IOrder, etc .... it should be shared!

How can I get the shape element of real properties? Only with reflection?

+3
source share
4 answers

Oded is right , he does not seem (to me or me) to make sense to try and make this method general. You are trying to generalize a method whose functionality is specific to several types.

, , , , . : , , , :

- :

void BindElements<T, TProperty>(IEnumerable<T> dataObjects,
                                Func<T, TProperty> selector)
{
    Paragraph para = new Paragraph();

    foreach (T item in dataObjects)
    {
       // Notice: by delegating the only type-specific aspect of this method
       // (the property) to (fittingly enough) a delegate, we are able to 
       // package MOST of the code in a reusable form.
       var property = selector(item);

       InlineUIContainer uiContainer = this.CreateElementContainer(property)
       para.Inlines.Add(uiContainer);
    }

    FlowDocument flowDoc = new FlowDocument(para);
    this.Document = flowDoc;
}

, , , IPerson, (, , , ):

public void BindPeople(IEnumerable<IPerson> people)
{
    BindElements(people, p => p.FirstName);
}

... IOrder:

public void BindOrders(IEnumerable<IOrder> orders)
{
    BindElements(orders, o => p.OrderNumber);
}

... ..

+6

(, IPerson), , :

public void BindElements<T>(IEnumerable<T> dataObjects) where T : IPerson

IPerson FirstName LastName peroperties, T.

. .

+4

Dan, Func<T, TProperty> selector , selector , T TProperty. , , BindElements , , ,

string CreatePersonElement(IPerson person) {
    return string.Format("{0} {1}", person.FirstName, person.LastName);
}

TProperty string T IPerson. BindElements

BindElements(myPersonCollection,CreatePersonElement);

myPersonCollection , List<T>. foreach

foreach (T item in dataObjects) {
   // Notice: by delegating the only type-specific aspect of this method
   // (the property) to (fittingly enough) a delegate, we are able to 
   // package MOST of the code in a reusable form.
   var property = selector(item);

   InlineUIContainer uiContainer = this.CreateElementContainer(property)
   para.Inlines.Add(uiContainer);
}

property TProperty, CreatePersonElement string. a string , , CreateElementContainer .

You will then have one of these methods to go into the second parameter for BindElementsfor each type that you want to support (i.e. ICustomer, IOrder).

+3
source

I would read http://msdn.microsoft.com/en-us/library/d5x73970.aspx and think about the Oded answer again.

0
source

Source: https://habr.com/ru/post/1777199/


All Articles