Method to create Project Variables from c#

  • Автор темы Автор темы lokiys
  • Дата начала Дата начала

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 922
Реакции
1 206
Баллы
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
i ms tuck today in a situation like this too it will be very handy if it can be implemented +1 for this
 
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;
 
Hmm i think a setter would be more helpful though also thanks for the reflection code :)
 
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");
 
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 ?
 
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.
 
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"] :)
 
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

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