Archive for June 26th, 2012
Constructors and Destructors (C#)
Posted by Rajanihanth in .Net, Tech Tips-.Net on June 26, 2012
This is for my quick references and I have taken most of the information from MSDN and other web sites. BTW I have written and tested the source!
Constructors
– Constructors are methods that are executed when an object of a class is created.
– They have the same name as the class.
– A class may have multiple constructors that take different arguments.
– Constructors enable the programmer to set default values.
– Like class, structs also can have constructors.
There are 3 kind of constructors in .Net (as far as I know), they are
- Instance Constructors -Used to create and initialize instances of the class
- Private Constructors – A special type of instance constructor that is not accessible outside the class (cannot be instantiated)
- Static Constructors – Automatically initialize the class before the first instance is created or any static members are referenced
public class A //Class Name A { public int i; public A() // Constructor of A { i=1; } }
class Program { static void Main(string[] args) { A a = new A(); //Initiated with new Operator Console.WriteLine("The initialized value is: {0}", a.i); System.Console.ReadLine(); //This line is to stop the result window } }
Destructors
– are used to destruct instances of classes.
– cannot be defined in structs. They are only used with classes.
– A class can only have one destructor.
– cannot be inherited or overloaded.
– cannot be called. They are invoked automatically
– does not take modifiers or have parameters.
class A { public A() { Console.WriteLine("Constructor"); } ~A() { Console.WriteLine("Destructor"); } }
class Program { static void Main(string[] args) { A a = new A(); //Initiated with new Operator System.Console.ReadLine(); //This line is to stop the result window } }
Access is denied: ‘xxx.dll’ – Manually add an assembly (.dll) to the GAC on Windows Server 2008 R2
Posted by Rajanihanth in .Net, GAC, SharePoint 2007, SharePoint 2010, Webpart on June 26, 2012
I supposed to post this issue few months ago, but I don’t really know why this has been in my draft folder till this date! When we were migrating our servers to new environment, we deployed some custom web parts manually. While deploying the web part (Drag and drop the assembly (web part .dll) into Global Assembly Cache (GAC) folder) I have got the following wired error message! 😦
GAC normally located in C:\Windows\assembly directory, and ‘WebPartStepBiStep.dll‘ is my web part assembly.
After spending some time on the web, I have got the solution here. Actually the ‘User Account Control: run all administrators in Admin Approval Mode’ was Enabled on the Local Security Policy. Which means the local administrators group required Admin Approval Mode (AAM) to perform these kind of operations. Here is the definition for Admin Approval Mode (AAM) from MicroSoft site.
Admin Approval Mode: AAM is a User Account Control (UAC) configuration in which a split user access token is created for an administrator. When an administrator logs on to a Windows Server 2008, the administrator is assigned two separate access tokens. Without AAM, an administrator account receives only one access token, which grants that administrator access to all Windows resources.
To access the Local Security Policy, either we can goto the Administrative Tools –> Local Security Policy OR we can run the ‘secpol.msc’ on the command prompt.
Start –> Administrative Tools –> Local Security Policy
Click Start –> Cmd –> type ‘secpol.msc’ then enter
You will be getting the Local Security Policy window like below and the highlighted is the AAM.
So the solution for our problem is Disabled the User Account Control. To do this double click on the highlighted policy on the Local Security Policy above and then you will be getting this window.
Click Disabled and OK.
After disabling this you will be able to drag and drop the .dll to the GAC.
Please Note: 1. Rebooted required for changes to the local security policy on the server.
2. Disabling this can make security problems in your environment, so after completing task, you should be enabled back the UAC.
That’s all guys, Happy drag and dropping..!
Thanks. R/.
References: