Add a reference to Microsoft.VisualBasic.dll, add the following code in a pas file to your project, and you have the VB My namespace:
namespace <your default namespace>;
interface
uses
Microsoft.VisualBasic.*;
type My = assembly static class
private
fApplication: MyApplication := MyProject.m_AppObjectProvider.GetInstance;
fComputer: MyComputer := MyProject.m_ComputerObjectProvider.GetInstance;
fSettings := PaysafeReconciliation.Properties.Settings.Default;
fUser: Microsoft.VisualBasic.ApplicationServices.User := MyProject.m_UserObjectProvider.GetInstance;
Assembly
property Application: MyApplication read fApplication;
property Computer: Microsoft.VisualBasic.Devices.Computer read fComputer;
property Settings: Properties.Settings read fSettings;
property User: Microsoft.VisualBasic.ApplicationServices.User read fUser;
end;
type MyComputer = assembly class(Microsoft.VisualBasic.Devices.Computer);
type MyApplication = assembly class(Microsoft.VisualBasic.ApplicationServices.ApplicationBase);
type MyProject = assembly static class
unit
var m_ComputerObjectProvider := New MyProject.ThreadSafeObjectProvider<MyComputer>();
var m_AppObjectProvider := New MyProject.ThreadSafeObjectProvider<MyApplication>();
var m_UserObjectProvider := New MyProject.ThreadSafeObjectProvider<Microsoft.VisualBasic.ApplicationServices.User>();
end;
type ThreadSafeObjectProvider<T> nested in MyProject = unit class
private
var m_Context:= New Microsoft.VisualBasic.MyServices.Internal.ContextValue<T>(); readonly;
assembly
property GetInstance: T read if m_Context.Value = nil then Activator.CreateInstance<T>() else m_Context.Value;
public
end;
implementation
end.
Free to use for anyone that is interested - this code gives access to all computer resources (like the filesystem), the current user, the application settings and application properties.
The My.Webservices need generated code, so this one is not supported.
Done, for vNext, except for Settings. I’ll need exists() support n the compiler before I can do that, because not every project HAS a Settings class (yet).
For now, any project that references VB.dll will automatically get this:
namespace %rootnamespace%;
uses
Microsoft.VisualBasic.*;
type
My = assembly partial static class
assembly
property Application: MyApplication := MyProject.AppObjectProvider.GetInstance; readonly; lazy;
property Computer: Microsoft.VisualBasic.Devices.Computer := MyProject.ComputerObjectProvider.GetInstance; readonly; lazy;
//property Settings: Properties.Settings := %rootnamespace%.Properties.Settings.Default; readonly; lazy;
property User: Microsoft.VisualBasic.ApplicationServices.User := MyProject.UserObjectProvider.GetInstance; readonly; lazy;
end;
MyComputer = assembly class(Microsoft.VisualBasic.Devices.Computer);
MyApplication = assembly class(Microsoft.VisualBasic.ApplicationServices.ApplicationBase);
MyProject = assembly static class
unit
property ComputerObjectProvider := new MyProject.ThreadSafeObjectProvider<MyComputer>(); readonly; lazy;
property AppObjectProvider := new MyProject.ThreadSafeObjectProvider<MyApplication>(); readonly; lazy;
property UserObjectProvider := new MyProject.ThreadSafeObjectProvider<Microsoft.VisualBasic.ApplicationServices.User>(); readonly; lazy;
end;
ThreadSafeObjectProvider<T> nested in MyProject = unit class
private
var fContext := new Microsoft.VisualBasic.MyServices.Internal.ContextValue<T>(); readonly;
assembly
property GetInstance: T read if fContext.Value = nil then Activator.CreateInstance<T>() else fContext.Value;
end;
end.
(you can add Settings via second partial of there class, for now)
I can type it myself, but it means I have to type ESC after each word to prevent the CC to replace the words.
Without that, My.Computer.FileSystem becomes Microsoft.VisualBasic.MyGroupCollectionAttribute.Computer.FileSystem
CC works for vNext, Fire and Water. This new logic does not just get used for the My.pas file, but also for any other source generated behind the scenes during build (eg R.java files on Android, and more).
In 2313 with VS2017, the CC does not work.
If you type My. nothing happens. When you press Ctrl-Space to open the CC, the Oxygene output prints: Unknown identifier “My”
I don’t know what the status of this for VS is, iirc the issue is still open. I just fixed Fire/Water, as the VS IDE isn’t my area. it works there, and that’s what my previous post referred to ;).