Closed ycherkes closed 2 years ago
Not sure what is the expected result. Can you provide the source and target type definitions? I think you selected the wrong action - "Generate explicit conversion" instead of "Create mapping lambda"
Explicit conversions https://docs.mappinggenerator.net/explicit-conversions/explicit-conversion-codefix/ Create mapping lambda https://docs.mappinggenerator.net/mappings/mapping-lambda/
You can see them in the Solution Explorer. I expanded them. And yes, I've selected "Generate explicit conversion" on ...Select(x => x ). It's highlighted on a picture.
Would you mind trying to use "Create mapping lambda" and let me know if the result meets your expectations?
"Create mapping lambda" works fine for me.
I've just released a fix for a problem with the explicit conversion. Please update your MappingGenerator to v2021.10.32
Please let me know if that solved your problem.
I've just tested it - works fine for me.
But if it's possible, it would be much better to have something like this:
Arrange
Auto-generating
Manual fix
Is it possible?
I'm not sure if I understand correctly your requirement. Do you want to have an option to generate (x, i) =>
instead of (x) =>
??
No. Step 1 is a manual step, Step 2 - is a MappingGenerator step. So I want the MappingGenerator to behave inside the lambda in the same way as inside the function.
Example:
Class2 Map(Class1 class1)
{
return class1;
}
Then I'm clicking on a class1 and select the "Generate explicit conversion" auto-suggestion. The MappingGenerator generates:
Class2 Map(Class1 class1)
{
return new Class2
{
Foo = class1.Foo,
// TODO: Foo_Foo = ???
// TODO: Index = ???
};
}
I'd like to have the same for a lambda:
var c1 = new[]{ new Class1{ Foo = "Test" } };
IEnumareble<Class2> c2 = class1.Select(x => /* click here -> */ x);
Then I'm clicking on x in the return part of lambda and select the "Generate explicit conversion" auto-suggestion. The MappingGenerator should generate:
var c1 = new[]{ new Class1{ Foo = "Test" } };
IEnumareble<Class2> c2 = class1.Select(x => new Class2
{
Foo = x.Foo,
// TODO: Foo_Foo = ???
// TODO: Index = ???
});
Now MappingGenerator generates the following code:
IEnumerable<Class2> c2 = c1.Select(x => x).Select(class1 => new Class2
{
Foo = class1.Foo
// TODO: Foo_Foo = ???
// TODO: Index = ???
});
The CS0266
is reported by the compiler for the whole expression c1.Select(x => x)
not just for the x
. Generate explicit conversion
is a codefix that responds to CS0266
hence the behavior that you observe.. For what you are trying to achieve, you should use the Create mapping lambda
option or use the Generate explicit conversion
for the following expression (even less typing):
var c1 = new[]{ new Class1{ Foo = "Test" } };
IEnumareble<Class2> c2 = /* click here -> */ c1;
OK,
I can explicitly define the output result of the Select method:
var c1 = new[]{ new Class1{ Foo = "Test" } };
IEnumareble<Class2> c2 = class1.Select<Class2>(x => (x => /* click here -> */ x);
The CS1061 is reported in this case:
Severity Code Description Project File Line Suppression State Error CS1061 'Class1[]' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'Class1[]' could be found (are you missing a using directive or an assembly reference?) Mapper C:\Users\zen\Source\Repos\Mapper\Program.cs 21 Active
Can this explicit conversion suggestion and codefix be linked to the CS1061 error code?
I don't understand why you don't want to use the existing solutions and you try to make the problem even more complicated.. CS1061
represents totally different set of errors that has nothing to do with type conversion. Can you provide your real use case as maybe this fabricated example makes things a little too hard to understand for me?
Your solution is fine for me. I just tried to adjust it with my habits :) Thank you. This issue can now be closed. Because now it doesn't generate the wrong C# code.