Method to create Project Variables from c#

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 770
Благодарностей
1 182
Баллы
113
Would be good method to create project variables from c#
Now we have to create Project variables before assign value, Would be good if could do something like

string password = Set.project.Variables["password"].Value

Or if there is no such variable in Project then it could be created automatically

thanks
 
  • Спасибо
Реакции: surrealmix и bigcajones

sandeep

Новичок
Регистрация
26.10.2016
Сообщения
24
Благодарностей
5
Баллы
3
i ms tuck today in a situation like this too it will be very handy if it can be implemented +1 for this
 

Adigen

Client
Регистрация
28.07.2014
Сообщения
825
Благодарностей
651
Баллы
93
Would be good method to create project variables from c#
Now we have to create Project variables before assign value, Would be good if could do something like

string password = Set.project.Variables["password"].Value

Or if there is no such variable in Project then it could be created automatically

thanks
You can do it runtime, using reflection, but i don,t recommend.
C#:
string variableName = "MyVariableName"; //new variable name
string variableValue = "MyVariableValue"; //new variable value
object obj = project.Variables;
obj.GetType().GetMethod("QuickCreateVariable").Invoke(obj,new Object[]{variableName});
project.Variables[variableName].Value = variableValue;
 

sandeep

Новичок
Регистрация
26.10.2016
Сообщения
24
Благодарностей
5
Баллы
3
Hmm i think a setter would be more helpful though also thanks for the reflection code :-)
 

Adigen

Client
Регистрация
28.07.2014
Сообщения
825
Благодарностей
651
Баллы
93

Adigen

Client
Регистрация
28.07.2014
Сообщения
825
Благодарностей
651
Баллы
93
You can use this little extention in OwnCode
C#:
public static class Extentions
{
    public static void Create(this ILocalVariables variables, string name, string value)
    {
         variables.GetType().GetMethod("QuickCreateVariable").Invoke(variables,new Object[]{name});
         variables[name].Value = value;
     }
}
Insert it after:
C#:
namespace ZennoLab.OwnCode
{
Now you can add variables using this method:
C#:
project.Variables.Create("test","1234");
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 770
Благодарностей
1 182
Баллы
113
You can do it runtime, using reflection, but i don,t recommend.
C#:
string variableName = "MyVariableName"; //new variable name
string variableValue = "MyVariableValue"; //new variable value
object obj = project.Variables;
obj.GetType().GetMethod("QuickCreateVariable").Invoke(obj,new Object[]{variableName});
project.Variables[variableName].Value = variableValue;

Nice trick. :-)
Why do not you recommend to do that ?
 

Adigen

Client
Регистрация
28.07.2014
Сообщения
825
Благодарностей
651
Баллы
93
Nice trick. :-)
Why do not you recommend to do that ?
Method is not public, so it can be changed any time, and reflection have a higher cost:
Код:
Reflection requires a large amount of the type metadata to be loaded and then processed.
This can result in a larger memory overhead and slower execution.
According to tests property modification is about 2.5x-3x slower and method invocation is 3.5x-4x slower.
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 770
Благодарностей
1 182
Баллы
113
Method is not public, so it can be changed any time, and reflection have a higher cost:
Код:
Reflection requires a large amount of the type metadata to be loaded and then processed.
This can result in a larger memory overhead and slower execution.
According to tests property modification is about 2.5x-3x slower and method invocation is 3.5x-4x slower.

Thanks for explanation. :-) Anyway this feature is already implemented as project.Context["anyVariable"] :-)
 

Adigen

Client
Регистрация
28.07.2014
Сообщения
825
Благодарностей
651
Баллы
93
Thanks for explanation. :-) Anyway this feature is already implemented as project.Context["anyVariable"] :-)
project.Context and project.Variable are not the same;
project.Context type is dynamic, but project.Variable is string
project.Variable available from standart actions, project.Context only from snippets.

And yes, for snippets project.Context is very useful :ay:
 
  • Спасибо
Реакции: lokiys

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)