Quantcast
Channel: YouTrackReSharper (RSRP) - Bug and Issue Tracker
Viewing all articles
Browse latest Browse all 106942

RSRP-288216: Making class from anymous type

$
0
0
Reporter Andrey Zakharov (Andrey.Zakharov) Andrey Zakharov (Andrey.Zakharov)
Created Feb 9, 2012 12:36:54 PM
Updated Feb 9, 2012 3:01:12 PM
Resolved Feb 9, 2012 3:01:12 PM
Priority Normal
Type Feature
Fix versions No Fix versions
State Won't fix
Assignee Sergey Shkredov (serjic.shkredov)
Subsystem Refactoring
Affected versions No Affected versions
Fixed in build No Fixed in build
It would be good to have a refactoring option to generate simple class based on anonymous class declaration. For example I have following code generationg JSON result in ASP.NET MVC controller:
return Json(new{
SomeField="SomeValue",
SomeOtherField=new[]{1,2,3},
SomeIncapsulatedAnonymous=new{ExtraField="abc"}
});

For some reason (personally I got to the point when I needed IEqualityComarer implementation) I decide not to use anonymous types anymore and create a named class instead. It would be nice if resharper let me do this in one click.

return Json(new JsonViewModel{
SomeField="SomeValue",
SomeOtherField=new[]{1,2,3},
SomeIncapsulatedAnonymous=new{ExtraField="abc"}
});
.....
internal class JsonViewModel{
public string SomeField;
public IEnumerable<int> SomeOtherField;
public object SomeIncapsulatedAnonymous;
}


Or even better if refactoring option takes in account anonymous nesting (or even giving contol over it to user):

return Json(new JsonViewModel{
SomeField="SomeValue",
SomeOtherField=new[]{1,2,3},
SomeIncapsulatedAnonymous=new JsonSomeIncapsulatedAnonymousViewModel{ExtraField="abc"}
});
.....
internal class JsonViewModel{
public string SomeField;
public IEnumerable<int> SomeOtherField;
public JsonSomeIncapsulatedAnonymousViewModel SomeIncapsulatedAnonymous;
}
internal class JsonSomeIncapsulatedAnonymousViewModel{
public string ExtraField;
}


And the final proposal is to give an option to generate contructor for abovementioned classes:
return Json(new JsonViewModel("SomeValue",new[]{1,2,3},new JsonSomeIncapsulatedAnonymousViewModel("abc")));

.....
internal class JsonViewModel{
public string SomeField;
public IEnumerable<int> SomeOtherField;
public JsonSomeIncapsulatedAnonymousViewModel SomeIncapsulatedAnonymous;
public JsonViewModel(string someField,IEnumerable<int> someOtherField,JsonSomeIncapsulatedAnonymousViewModel someIncapsulatedAnonymous){
SomeField=someField;
....
}
}
internal class JsonSomeIncapsulatedAnonymousViewModel{
public string ExtraField;
public JsonSomeIncapsulatedAnonymousViewModel(string extraField){
ExtraField=extraField
}
}

Viewing all articles
Browse latest Browse all 106942

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>