Category: "Example Codes"
Dynamically Create an instance of class in code
In C#, you can create an instance of class dynamically. For example, you can create an instance of class according to database values, rather than hard coded.
You just need to use reflection.
Code
ObjectHandle obj = Activator.CreateInstance(AssemblyName,className); | |
Controller = (IApp) obj.Unwrap(); |
Please note that, Assembly Name is without ".dll", such as "AdvgenContact.App.WPF", and the class name need to be full qualified, for example, "AdvgenContact.App.WPF.MainApp".
If you wish to know more, please check out our open source project, AdvgenContact.
Singleton in C#
To implement Singleton Pattern in C#, that is easy. I like to static class. For example:
Code
public static class GobalData | |
{ | |
public static Group Group {get;set;} | |
} |
HOWTO: Find a Physical Path in ASP.Net from URL
In a number of situations, we need to know a Physical Path in a server from URL. For example, we need to upload some photo. Thus, this is a very common task, and is very easy to do in C#
Code
string serverPath = Server.Map("~/Photo"); // e.g. it returns "C:\site\Photo" |
Edit Connection String in code and save into web.config
I am building an installer for my CMS, AdvGenCMS. The first part is adding connection string and saves it into web.config. That is doable in C#. Just like the following code:
Code
Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); | |
ConnectionStringsSection connectionStrSection = config.GetSection("connectionStrings") as ConnectionStringsSection; | |
string conntectStr = String.Format("data source={0};Initial Catalog={1};uid={2};password={3};Intergrated Security=SSPI",serverModel.ServerName,serverModel.DatabaseName,serverModel.UserName,serverModel.UserPassword); | |
if (connectionStrSection != null) | |
{ | |
if ( connectionStrSection.ConnectionStrings["AdvCMS.Data.Linq.Properties.Settings.AdvCMSConnectionString"] == null) | |
{ | |
ConnectionStringSettings setting = new ConnectionStringSettings(); | |
setting.Name = "AdvCMS.Data.Linq.Properties.Settings.AdvCMSConnectionString"; | |
connectionStrSection.ConnectionStrings.Add( | |
setting); | |
} | |
connectionStrSection.ConnectionStrings["AdvCMS.Data.Linq.Properties.Settings.AdvCMSConnectionString"].ConnectionString = conntectStr; | |
connectionStrSection.ConnectionStrings["AdvCMS.Data.Linq.Properties.Settings.AdvCMSConnectionString"].ProviderName = "System.Data.SqlClient"; | |
config.Save(); | |
} |
We can use WebConfigurationManager.OpenWebConfiguration("~") to get the whole configuration and then use config.GetSection("connectionStrings") to get the connection string section. Now, we can play around the connection string section freely.
JSON in WCF
A lot of people know about WCF supports XML as the return response. Actually, it supports JSON. All you need to set ResponseFormat.
Code
[WebGet(ResponseFormat=WebMessageFormat.Json)] | |
[OperationContract] | |
public SessionKeyDTO Authenicate(string username, string password) |
And you need to set web.config
Code
<behavior name="AdvCMS.Services.Service2AspNetAjaxBehavior"> | |
<webHttp/> | |
<enableWebScript /> | |
</behavior> |
It needs <webhttp/>. That is easy!
The code is a part of our CMS, AdvGenCMS.