workflow foundation - Dynamically setting variables in outer scope from WF 4 activity -


how can dynamically set values of variables parent scope in windows workflow foundation activity under .net 4?

an attempt failed (drop on sequence activity on workflow sequence has int variable named test):

public sealed class codeactivity1 : nativeactivity {     protected override void cachemetadata(nativeactivitymetadata metadata)     {         _locationreferences =             metadata.environment.getlocationreferences().tolist();          base.cachemetadata(metadata);     }      protected override void execute(nativeactivitycontext context)     {         locationreference locationreference =             _locationreferences.find(                 x => x.name == "test" && x.type == typeof (int));          if (locationreference != null)         {             console.writeline(                 locationreference.name + " " + locationreference.type);              // blows here.             location location = locationreference.getlocation(context);             location.value = 5;         }     }      private list<locationreference> _locationreferences; } 

this results in:

system.invalidoperationexception unhandled user code
message=activity '1.2: codeactivity1' cannot access variable because declared @ scope of activity '1.1: sequence'. activity can access own implementation variables.

it find variable; can't or set value.

the variable name ("test" in above example) not known until runtime.

the normal way of handling define outargument , in workflow designer bind outargument variable. in activity work argument. using nativeactivity gives outargument named result adding property of outargument fine.

another benefit don't need know "magic" variable names store results in.

update because code in comment below unreadable.

try adding following before line blows up:

var pi = context.gettype().getproperty("allowchainedenvironmentaccess", bindingflags.nonpublic | bindingflags.instance);  pi.setvalue(context, true, null);  

totally not supported use care :-)


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -