jeudi 24 mai 2018

How do I create a List

I have an ASP.NET MVC Application.

I have an example class called Example

public class Example
{
    public int Id { get; set; }
    public string Property1{ get; set; }
    public string Property2{ get; set; }
} 

I also have a test method - TestingCallFromSeparateConsoleApp that does some functionality and returns a List of Examples

public List<Example> TestingCallFromSeparateConsoleApp()
{
    List<Example> exampleList = new List<Example>();

    exampleList.Add(new Example
    {
        Id = 1,
        Property1 = "First",
        Property2 = "First",
    });

    exampleList.Add(new Example
    {
        Id = 2,
        Property1 = "Second",
        Property2 = "Second",
    });

    return exampleList;
}

I have a separate console application that is loading the MVC DLL manually.

I can call the TestingCallFromSeparateConsoleApp method and it returns the the result as type object.

I am trying to cast the result to its correct type of List of Examples.

 asm = Assembly.LoadFrom(assemblyFile);
 var smsAppInstance = asm.CreateInstance("EngineeringAssistantMVC.Controllers.TestController", false, BindingFlags.CreateInstance, null, null, null, null);

 var ExampleClass = asm.GetType("EngineeringAssistant.Controllers.Example", true);
 var ec = ExampleClass.GetType();

This gets the Example class details but I still cannot figure out how to actually instantiate and use a class of type "Example"

In my console How do I basically say:

ExampleClass newExampleClass = new ExampleClass();

So I can then say:

var result = smsAppInstance.GetType().GetMethod("TestingCallFromSeparateConsoleApp").Invoke(smsAppInstance, argumentsTopass);

Where result can be cast to List<ExampleClass>





Aucun commentaire:

Enregistrer un commentaire