1

I need to do this using reflection:

@Html.Grid((IEnumerable<MyType>)list).Columns(columns =>
           {
                 columns.Add(foo => foo.Title)
                        .Titled("Custom column title")
                        .SetWidth(110);
                 columns.Add(foo => foo.Description)
                        .Sortable(true);
           }).WithPaging(20)

Now I have var g which is object created after call @Html.Grid((IEnumerable<Type>)Model) using reflection. This is done by reflection because list contains object of class created at runtime and I need to call Grid(list) with defined type(which didn't exist at compile time):

var method = typeof(GridMvc.Html.GridExtensions).GetMethods()
                        .Where(mi => mi.Name == "Grid")
                        .ElementAt(0)
                        .MakeGenericMethod(new Type[] { t });
var g = method.Invoke(null, new object[] { Html, list });

So I need do something like:

g.Columns(columns =>
               {
                     columns.Add(foo => foo.Title)
                            .Titled("Custom column title")
                            .SetWidth(110);
                     columns.Add(foo => foo.Description)
                            .Sortable(true);
               }).WithPaging(20)

using reflection.

var g is type of HtmlGrid:

https://github.com/leniel/Grid.Mvc/blob/master/GridMvc/Html/HtmlGrid.cs

Can someone provide example code to do this?


To be on the safe side I am adding Grid.Mvc github link: https://github.com/leniel/Grid.Mvc/blob/master/GridMvc/ because I don't know the way to solve it

6
  • Can you give more information about why you're trying to call this by reflection? Assuming this is GridMvc, I can only see a property called Columns rather than a method anyway... can you give a reference to the type you're using? Commented Feb 28, 2015 at 11:06
  • Can't you just cast g to the correct type and call the method normally? Commented Feb 28, 2015 at 11:11
  • How do you know that the type t has a Title and a Description properties? Commented Feb 28, 2015 at 11:16
  • Because doing it fully through reflection gets quite ugly. The foo => foo.Title will have to be built through Expression trees. Commented Feb 28, 2015 at 11:23
  • @xanatos Can you indicate me how do you mean it? Some example? Commented Feb 28, 2015 at 11:37

1 Answer 1

1

Third time is the charm :)

This time I tested it.

You use it like

@Html.MakeGrid(items, someType)

where someType is the Type of your objects.

or even

this.Write(Helper.MakeGrid(Html, items, someType);

or in any way you would use your @Html.Grid method.

I cheat twice: once I call through reflection from the non-generic MakeGrid a generic MakeGridInternal, and the second time in CreateColumns where I use reflection to get the PropertyInfo of the various columns.

Note that when calling MakeGrid you must pass the real type of your elements in type, and items must be a collection of any reference type (but 90% it will work if you change the signature to object items)

public static class Helper {
    public static IHtmlString MakeGrid(this HtmlHelper html, IEnumerable<object> items, Type type) {
        // You can give even this signature:
        // public static IHtmlString MakeGrid(HtmlHelper html, object items, Type type)
        // But clearly items MUST be a collection of some type!
        return (IHtmlString)typeof(Helper).GetMethod("MakeGridInternal").MakeGenericMethod(type).Invoke(null, new object[] { html, items });
    }

    public static IHtmlString MakeGridInternal<T>(HtmlHelper html, IEnumerable<T> items) where T : class {
        return GridMvc.Html.GridExtensions.Grid<T>(html, items)
                 .Columns(CreateColumns)
                 .WithPaging(10);
    }

    public static void CreateColumns<T>(IGridColumnCollection<T> columns) {
        Type t = typeof(T);

        PropertyInfo title = t.GetProperty("Title");
        PropertyInfo description = t.GetProperty("Description");

        columns.Add(title)
            .Titled("MyTitle")
            .SetWidth(100);

        columns.Add(description)
            .Sortable(true);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

It's nice idea, thanks. I will try it after weekend :)
Hmm.....I get error: property is not defined for type Int32 id system.object. Is possible to fix it some easy way?:)
@mrfazolka Sadly no I think
Ok, don't, never mind :) I have backup solutiom :) I have backup solution :)
@mrfazolka Try now :-) I thought about the problem, installed the MVC and solved it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.