Inside Microsoft® Visual Studio® .NET 2003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Inside Microsoft® Visual Studio® .NET 2003 [Electronic resources] - نسخه متنی

Brian Johnson

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










Persisting Solution and Project Information Across
IDE Sessions


At times, your add-in or macro might need to save some data that should be carried along with the solution or project file. The object model supports saving information into these files with the EnvDTE.Globals object. You can find this object by calling the Globals property of both of these objects:


Sub SolutionGlobals()
Dim globals As EnvDTE.Globals
globals = DTE.Solution.Globals
End Sub
Sub ProjectGlobals()
Dim globals As EnvDTE.Globals
globals = DTE.Solution.Projects.Item(1).Globals
End Sub

The Globals object of the Solution and Project objects works in much the same way as the Globals object found on the DTE object, with a few minor differences. First, if a macro or an add-in stores data into the solution or project file, even if the VariablePersists flag is set for that variable the data might not be written into the solution or project file. This is because making a change to a variable causes the project or solution file to be put into a modified state. If you close the solution or project file but do not choose to save the modified files, the data won't be written into that file. Second, unlike the EnvDTE.Globals object of the DTE object, which can store data into a wide variety of formats, data stored into a solution or project file can be stored only in string format. This is because project and solution files are text-based, so any data stored into these files must also be in a text format. This doesn't mean that nonstring data can't be stored into the solution or project Globals object. It just means that when the data is to be written into the solution or project files, an attempt will be made to convert the data into a string. If that fails, the data won't be stored. Also, because the data is converted into a string when it is stored into the solution or project files, when the Globals object is restored from the solution or project file this data will also be in a string format. It is up to the macro or add-in code to properly determine which format the data is in.

Earlier in this chapter, you saw a scenario of a solution add-in in which every time a build was performed a counter was incremented to keep track of the number of builds. The Solution.Globals object provides a good place to store this value, as is demonstrated in the BuildCounter sample. This sample, when loaded into a solution, first connects to the OnBuildDone event. As each OnBuildDone event is fired, the sample checks for the existence of the BuildCounter variable within the solution Globals object. If this value exists, it is incremented and stored back into the Globals object. If this value doesn't exist, the value 1 is stored. The code for the OnBuildDone event is shown here:


void OnBuildDone(EnvDTE.vsBuildScope Scope, EnvDTE.vsBuildAction Action)
{
//Increment the build counter by storing a value in the
// solution file through the Globals object:
Globals globals;
globals = applicationObject.Solution.Globals;
if(globals.get_VariableExists("BuildCounter"))
{
//A counter has been set, increment it:
System.Int32 int32;
int32 = System.Int32.Parse((string)globals["BuildCounter"]);
int32++;
globals["BuildCounter"] = int32.ToString();
}
else
{
//The variable has never been set, seed the counter:
globals["BuildCounter"] = 1.ToString();
globals.set_VariablePersists("BuildCounter", true);
}
}


/ 118