0

I built a function to create forms for me based on the model I give it.

using MyProject.Models;
...
public partial class CRUDPage : ContentPage
{
    public CRUDPage()
    {
        InitializeComponent();
        Teacher teacher = new Teacher
        {
            id = 1,
            name = "John Smith",
            DOB = new DateTime(1995, 9, 12),
            place = "Hebron",
            salary = 20.5
        };
        //set the content of the page >>> PCL, Xamarin.forms
        Content = new FormGenerator<Teacher>().GenerateForm(teacher);
    }
}

I'm trying to make the code more dynamic by allowing to create forms using any type of models but with no luck. the code should be something like this.

public partial class CRUDPage : ContentPage
{
    public CRUDPage(object entity)
    {
        InitializeComponent();
        Content = new FormGenerator<typeof(entity) >().GenerateForm(entity);
    }
}

But the above code doesn't work; looks like Type is not the same as the Class itself.

How can I get the "class type" of an object to use it as a generic type in the function?

Abozanona
  • 2,261
  • 1
  • 24
  • 60
  • see this question: [How do I use reflection to call a generic method?](http://stackoverflow.com/q/232535/2803565) – S.Serpooshan Jan 23 '17 at 18:01
  • What is the logic of GenerateForm method. In that method any way you will know the type of the object being passed and you have access to all of it's properties using reflection which you can use to create the form. – Chetan Jan 23 '17 at 18:05
  • Can you show the code for FormGenerator's constructor and GenerateForm? – Scott Chamberlain Jan 23 '17 at 18:07
  • @ScottChamberlain It's a bit long code(more than 200 lines), here's a screen shoot of it. I hope it will be enough. [http://i.imgur.com/vsXoBoU.png](http://i.imgur.com/vsXoBoU.png) – Abozanona Jan 23 '17 at 18:43
  • @S.Serp I tried the answers in the page you linked but those two errors keeps appearing. `'Type' does not contain a definition for 'GetMethod' and no extension method 'GetMethod' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)` `cannot convert from 'object' to 'System.Type'` – Abozanona Jan 23 '17 at 18:55
  • Does `mform` need to be T? Would your code still work if it was `object`? If so you can get rid of the generics and solove the problem. – Scott Chamberlain Jan 23 '17 at 19:00
  • @ScottChamberlain No it doesn't. I changed it to object and got rid of generics and the problem was solved :D Thanks a tot. – Abozanona Jan 23 '17 at 19:09
  • Converted my comment in to an answer so you can accept it. – Scott Chamberlain Jan 23 '17 at 19:36

2 Answers2

1

Looking at your image of FormGenerator, does mform need to be T? Would your code still work if it was object? If so you can get rid of the generics and solve the problem.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
0

You'll have to use MakeGenericType to create an instance where the type is only known at runtime.

public CRUDPage(object entity)
{
    InitializeComponent();
    var type = typeof(FormGenerator<>).MakeGenericType(new [] { typeof(entity) });
    var instance = Activator.CreateInstance(type);
    Content = instance.GetType().GetMethod("GenerateForm").Invoke(instance, new[] { entity });
}

Note that this is much slower than using real generics.

Scott
  • 5,338
  • 5
  • 45
  • 70
  • Your code gives me two errors on the second and fourth lines. `entity' is a variable but is used like a type` `'Type' does not contain a definition for 'GetMethod' and no extension method 'GetMethod' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)` – Abozanona Jan 23 '17 at 18:29
  • @YazanWYusuf You may have to add `using System.Reflection`. If you're on .NET Core, it might be `GetTypeInfo()` IIRC. – Scott Jan 23 '17 at 19:33