Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Reflection Tip: Run Constructor even Activator.CreateInstance gives Exception

Featured Replies

Posted

Run Constructor even Activator.CreateInstance gives Exception

 

Assembly asm = Assembly.LoadFrom(@"X:\xxxasd.exe");
Type kd = asm.GetType("keydata");
// Create an instance of the type 
object classInstance = Activator.CreateInstance(keydata, null);

----------> This always throws errorr ..tried different approaches i.e with other parameters and Bindingflags ...

 

There is also System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(RuntimeTypeHandle type), which additionally guarantees that the static constructor is only called once, regardless how many times the method is called:

Type myClass = typeof(MyClass); System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(myClass.TypeHandle);

https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.runtimehelpers.runclassconstructor?redirectedfrom=MSDN&view=netcore-3.1#System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor_System_RuntimeTypeHandle_

 

Even though it is possible, it may not be a good idea to do that. However, if you access any member of the class, the runtime will invoke the static constructor automatically for you. For example:

// Read the field 'SomeValue', so that the runtime invokes static ctor Type myClass = typeof(MyClass); myClass.GetField("SomeValue").GetValue(null);

Since accessing a field of the class cannot cause any side-effect (other than initialization of the class and call to the static constructor), this should be a relatively safe way to do this in general (however, it will stil work only for classes with some static field). This has the benefit that it guarantees that the type constructor will be invoked at most once which is quite important.

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.