Jump to content

c# mapping parameters to class instance properties

Goldman
Go to solution Solved by madknight3,

While it's not exactly what you're asking for, there are tools that can generate the constructor code for you so you don't have to write it out yourself (example). You can also create your own generator.

 

More in line with your question, I don't know of an existing solution, but you might be able to create your own solution using reflection. Here are some resources that may or may not help with that. At the very least it should give you an idea of the types of things you would want to search for.

http://stackoverflow.com/questions/214086/how-can-you-get-the-names-of-method-parameters

http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class

http://stackoverflow.com/questions/619767/set-object-property-using-reflection

Hey guys, lately i've discovered automapper exists, and while i was playing with it i got a question: can it also work for mapping parameters to the properties of an instance of a class(considering both the parameters and the properties have the same name and type)

 

hope i'm being clear, basically i have this code right now:

public Cliente CrearCliente(string Nombre, string Apellido, string Calle, int NumeroDomicilio, string Depto, string Piso, int DNI, string Email, string Fax, int Cuit, decimal TopeCtaCte, int TipoRentaMunicipalId, int TipoRentaProvincialId, int TipoSituacionImpositivaId, int Id, List<TelefonosClientes> TelefonosClientes)
    {
        return new Cliente() { Apellido = Apellido, Calle = Calle, Depto = Depto, DNI = DNI, Email = Email, Id = Id, Nombre = Nombre, NumeroDomicilio = NumeroDomicilio, Piso = Piso, TipoRentaMunicipalId = TipoRentaMunicipalId, TipoRentaProvincialId = TipoRentaProvincialId, TipoSituacionImpositivaId = TipoSituacionImpositivaId, TelefonosClientes = TelefonosClientes, Fax = Fax, Cuit = Cuit, TopeCtaCte = TopeCtaCte };
    }

and what i'm guessing is that there should be some way to make it something like:

public Cliente CrearCliente(string Nombre, string Apellido, string Calle, int NumeroDomicilio, string Depto, string Piso, int DNI, string Email, string Fax, int Cuit, decimal TopeCtaCte, int TipoRentaMunicipalId, int TipoRentaProvincialId, int TipoSituacionImpositivaId, int Id, List<TelefonosClientes> TelefonosClientes)
    {
        return something.something.createinstancefromparameters(Cliente)
    }

 

sorry for the possible crappy english, if the question is not clear(not really sure what's the best way to ask what i'm asking lol), maybe i wrote it more clearly in my stack overflow question:

http://stackoverflow.com/questions/43702275/is-there-any-way-to-automatically-map-properties-from-parameters-when-creating-a

 

tbh it's the first time i've had to ask a question there, normally things are easier to research

Link to comment
Share on other sites

Link to post
Share on other sites

While it's not exactly what you're asking for, there are tools that can generate the constructor code for you so you don't have to write it out yourself (example). You can also create your own generator.

 

More in line with your question, I don't know of an existing solution, but you might be able to create your own solution using reflection. Here are some resources that may or may not help with that. At the very least it should give you an idea of the types of things you would want to search for.

http://stackoverflow.com/questions/214086/how-can-you-get-the-names-of-method-parameters

http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class

http://stackoverflow.com/questions/619767/set-object-property-using-reflection

Link to comment
Share on other sites

Link to post
Share on other sites

Didn't think about reflection tbh, thanks. But according to this there is no way to access method data(only it's signature) through reflection(bummer since i almost had a code to do what i wanted).

Still, the second answer of the first link you posted had a solution, that while isn't as pretty as i would like, saves me the work of doing it all(i'm using VS 2015-2017 with no resharper) so that basically fixes my situation, thanks! =D

 

Anyway i'm kinda surprised a functionality like this one isn't included on automapper and stuff like that(maybe i'm the only one that finds this bothersome? dunno)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×