2015-06-03 6 views
2

У меня есть активность с переменными (которые являются выражениями C#), но не в состоянии читать их значения.wf4 Активность может иметь доступ только к собственным переменным реализации

public Collection<Variable> Variables { get; } = new Collection<Variable>(); 

    protected override void DoExecute(NativeActivityContext context) 
    { 
     var x = Variables.FirstOrDefault(...).Get(context); 
    } 

в результате

Activity '1.1: MyActivity' cannot access this variable 
because it is declared at the scope of activity '1.1: MyActivity'. 
An activity can only access its own implementation variables. 

я попытался выставить их через cachemetadata

protected override void CacheMetadata(NativeActivityMetadata metadata) 
    { 
     metadata.SetImplementationVariablesCollection(Variables); 
    } 

И это приводит к

Exception <System.NotSupportedException: 
Expression Activity type 'CSharpValue`1' requires compilation in order to run. 
Please ensure that the workflow has been compiled. 

Мои переменные C# выражения и скомпилирован с

var wwfActivity = ActivityXamlServices.Load(xamlReader, new ActivityXamlServicesSettings {CompileExpressions = true}); 

enter image description here

ответ

1

Я был в состоянии взломать вокруг него с

var var = context.DataContext.GetProperties()["variableName"]; 
var value = var.GetValue(context.DataContext) as Whatever; 

без переопределения метода CacheMetadata, но он чувствует себя лил странно;

+0

Он чувствует себя странно, потому что это странно. Почему вы хотите получить доступ к такой переменной? – Joao

+0

Если вы просеиваете мои вопросы в WF, вы найдете аналогичную ситуацию. Я не думаю, что у меня когда-нибудь было достойное решение, и я довел его до MS. – Will

0

Я думаю, что если вы находитесь внутри цикла while со счетчиком посередине и пытаетесь получить доступ к переменной типа «Number» + i.ToString, вы не получите правильный ответ. Он не будет оценивать i.ToString.

using System; 
using System.ComponentModel; 
using System.IO; 
using System.Runtime; 
using System.Activities.Validation; 
using System.Collections.Generic; 
using System.Windows.Markup; 
using System.Collections.ObjectModel; 
using System.Activities; 

namespace WorkflowConsoleApplication2 
{ 

public sealed class CodeActivity1 : CodeActivity 
{ 
    // Define an activity input argument of type string 
    [DefaultValue(null)] 
    public InArgument<string> Test 
    { 
     get; 
     set; 
    } 

    protected override void CacheMetadata(CodeActivityMetadata metadata) 
    { 
     RuntimeArgument textArgument = new RuntimeArgument("Test", typeof(string), ArgumentDirection.In); 
     metadata.Bind(this.Test, textArgument); 


     metadata.SetArgumentsCollection(
     new Collection<RuntimeArgument> 
     { 
      textArgument, 
     }); 
    } 

    // If your activity returns a value, derive from CodeActivity<TResult> 
    // and return the value from the Execute method. 
    protected override void Execute(CodeActivityContext context) 
    { 
     Console.WriteLine(this.Test.Get(context)); 
    } 
} 

Вам нужно будет добавить, что RuntimeArgument к ArgumentCollection, как показано выше.

Я получил это от моего ответил вопросом: While activity in WF 4 rehosted designer

 Смежные вопросы

  • Нет связанных вопросов^_^