I want to know how to get access to an extension method from within a base class by passing the generic type.
I have several different classes all containing ToModel and ToContract Funcs. These methods are to switch from Entity Framework type to Data Contracts and vice versa.
They all do the same repetitive calls so I would like to condense down the code.
I have tried invoking functions using reflection. And many other methods to none avail. I have simplified the problem that I am facing in the following code.
My problem is that I cannot access the extension method from within the base class. Please help.
Error I am receiving: Class Does not contain a definition for 'ToModel' method.
Simplified Code
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestExtensions
{
public class Program : Base<Client>
{
static void Main(string[] args)
{
Client c = new Client();
c.FirstName = "First Name";
Console.WriteLine(c.FirstName);
c.ToModel();
Console.WriteLine(c.FirstName);
Program p = new Program();
p.go(c);
}
public void go(Client c)
{
base.ChangeNameAgain(c);
Console.WriteLine(c.FirstName);
}
}
}
Extension Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestExtensions
{
public static class ExtensionClass
{
public static Client ToModel(this Client c)
{
c.FirstName = "First Name Changed";
return c;
}
}
}
Client Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestExtensions
{
public class Client
{
public string FirstName { get; set; }
}
}
Base Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestExtensions
{
public class Base<T>
{
public T ChangeNameAgain(T c)
{
// This is where I need help
// Need to invoke the ToModel Method using reflection
return (T) c;
}
}
}
My actual code
Base
public class BaseApi<TContractType, TModelType> : ApiController
where TModelType : DataModelBase, IApiExtensionModel, new()
where TContractType: DataContractBase, IApiExtensionContract, new()
{
public TContractType Create(
IGenericRepository<TModelType> repo,
TContractType obj)
{ I
// This is what is giving me trouble
var instance = (TContractType)
Activator.CreateInstance(typeof(TContractType),
new object[] { obj });
var modelMap = instance.ToModel();
var ret = (dynamic)repo.Edit(modelMap);
return ret.ToContract();
}
}
Code that actually works
Trying to make it generic as it is repetitive.
[Microsoft.AspNetCore.Mvc.HttpPost]
public patientContract.Patient CreatePatient(
[Microsoft.AspNetCore.Mvc.FromBody] patientContract.Patient patient)
{
var map = patient.ToModel();
var ret = _patientRepo.Add(map);
return ret.ToContract();
}
Extensions
public static class PatientContractExtension
{
public static Model.Patient ToModel(this Contract.Patient patientContract)
{
var map = Mapper.Map<Contract.Patient, Model.Patient>(patientContract);
return map;
}
public static Contract.Patient ToContract(this Model.Patient patientModel)
{
var map = Mapper.Map<Model.Patient, Contract.Patient>(patientModel);
return map;
}