-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature - Allow specifying the constructor to use for activition #3
Comments
@michael-wolfenden thank you for your inquiry. Your feedback is appreciated. Now you can configure Remute to work with type with multiple constructors. You can do that providing Here is an example. public class User
{
public int Id { get; }
public string FirstName { get; }
public string LastName { get; }
public User(int id, string firstName, string lastName)
{
Id = id;
FirstName = firstName;
LastName = lastName;
}
public User(string firstNameNotMatchingPropertyName, string lastName)
{
FirstName = firstNameNotMatchingPropertyName;
LastName = lastName;
}
} var user = new User(1, "Joe", "Doe");
var constructor = user.GetType().GetConstructor(new[] { typeof(int), typeof(string), typeof(string) });
var config = new ActivationConfiguration().Configure(constructor);
var remute = new Remute(config); Also using var user = new User(1, "Joe", "Doe");
var type = user.GetType();
var constructor = type.GetConstructor(new[] { typeof(string), typeof(string) });
var firstNameParameter = constructor.GetParameters().Single(x => x.Name == "firstNameNotMatchingPropertyName");
var firstNameProperty = type.GetProperty("FirstName");
var config = new ActivationConfiguration()
.Configure(constructor, new Dictionary<ParameterInfo, PropertyInfo>() { [firstNameParameter] = firstNameProperty });
var remute = new Remute(config); So Last but not least, you can avoid reflection using the overloaded method. I think that would be the most preferable method because it's refactoring friendlier. var config = new ActivationConfiguration()
.Configure<User>(x => new User(x.FirstName, x.LastName))
.Configure<Foo>(x => ...)
.Configure<Bar>(x => ...);
var remute = new Remute(config); This expression is not actually executed. This is to point which constructor to use and how to map parameters. Please let me know if you have any questions. |
Remute can also search for a matching constructors from all those available ? |
Remute
assumes that the type has a single constructor. however we often have types with multiple constructors (usually one wasprivate
and only used for serialization).It would be nice to have a way to specify the specific constructor to use if there are multiple by providing a
Func<IEnumerable<ConstructorInfo>, ConstructorInfo>
or via some other mechanism.PS - Thanks for the library, it's been super useful to us
The text was updated successfully, but these errors were encountered: