c# - MSBuild: Describe Which Constructor to Call -
i'm working msbuild task needs instantiate class. class had 1 parameterless constructor, msbuild task needed type name instantiate class. have utilize case task run specific constructors, , don't know how handle in generic way. need instantiate different flavors of classa
:
public class classa { public classa() { } public classa(int someargument) { } public classa(int someargument, bool someotherargument) { } }
this original task looked like:
<dosomethingtask assembly="containsclassa.dll" type="classa" />
my ideal task phone call constructor has primitive types arguments:
<dosomethingtask assembly="containsclassa.dll" type="classa"> <constructorargs> <arg type="int">1</arg> <arg type="bool">true</arg> </constructorargs> </dosomethingtask>
i'm lost on search type of functionality. create string property called constructorargs
, parse using whatever format want, i'm hoping cleaner exists. help can provide!
edit - in case wondering, actual task i'm trying modify creates pregenerated view based on instantiated entity framework dbcontext. have our own dbcontext subclass various constructors, , we'd able phone call specific 1 during view generation.
you seek use reflection , constructor dbcontext subclass.
using system; using system.collections.generic; using system.linq; using system.reflection; using system.text; using microsoft.build.framework; using microsoft.build.utilities; namespace taskclass { // class created public class mydbcontext { public int constructorarg1 { get; set; } public string constructorarg2 { get; set; } public mydbcontext() { } public mydbcontext(int constructorarg1) { constructorarg1 = constructorarg1; } public mydbcontext(int constructorarg1, string constructorarg2) { constructorarg1 = constructorarg1; constructorarg2 = constructorarg2; } } // msbuild custom task public class dosomethingtask : task { public override bool execute() { var taskparameters = new taskparametersinfo(); taskparameters.extracttaskparametersinfo(this); var type = typeof(mydbcontext); constructorinfo ctor = type.getconstructor(taskparameters.types.toarray()); if (ctor == null) { // if constructor not found, throw error log.logerror("there no constructors defined these parameters."); homecoming false; } // create instance var mydbcontext = (mydbcontext)ctor.invoke(taskparameters.values.toarray()); homecoming true; } public int constructorarg1 { get; set; } public string constructorarg2 { get; set; } public string constructorarg3 { get; set; } // class handle task's parameters internal class taskparametersinfo { public list<type> types { get; set; } public list<object> values { get; set; } public taskparametersinfo() { types = new list<type>(); values = new list<object>(); } public void extracttaskparametersinfo(task task) { foreach (var property in task.gettype().getproperties(bindingflags.public | bindingflags.declaredonly | bindingflags.instance)) { var propertyvalue = property.getvalue(task, null); if (propertyvalue != null) { types.add(property.propertytype); values.add(propertyvalue); } } } } } }
import task on msbuild project:
<?xml version="1.0" encoding="utf-8" standalone="no" ?> <project toolsversion="4.0" defaulttarget="dosomething" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <usingtask taskname="taskclass.dosomethingtask" assemblyfile="taskclass\taskclass\bin\debug\taskclass.dll"/> <target name="dosomething"> <dosomethingtask constructorarg1="123" constructorarg2="are u talking me?" /> </target> </project>
hope helps.
c# .net msbuild msbuild-task
No comments:
Post a Comment