There are some things which are seemingly simple but are difficult to get done for the first time at least. This is one of them.

The other day I had to get a Stream from the String I had. Though it was apparently easy, it took me quite a while to figure it out. Nevertheless here’s what I did:

MemoryStream myStream = new MemoryStream(ASCIIEncoding.Default.GetBytes("My String"));

Recently I had a requirement where I had to create an XML file without any definitive structure but from a Class file. I had to write a tag for each class and a child property for each property.

Then instead of going for a primitive method of using the XML Document class, I went for LINQ to XML. The XElement makes life very easy when writing an XML. I just had to write 2 recursive methods to write the entire XML file.

This article helped a lot.

We do not have a built in split button in .net Framework. The work around is use tool strip split button which is not that useful in aesthetic perspective.

This is very good article where the author extended the standard Button control and modified it into a split button.

Modifying a Type at runtime is always difficult.

Requirement: Object must be assigned to a PropertyGrid. To expand the nested complex types in the Object, TypeConvertor attributes should be added to the properties of the type. Type should be modified not be modified at design time.

Solution: The PropertyGrid does not expand the nested complex types of an object assigned to it, if the complex type is a user-defined object. We need to add TypeConverterAttribute(typeof(ExpandableObjectConverter) to the user-defined types. Since I cannot modify the user-defined type at design time, it was not a feasible option. That leaves me with two alternatives:

Alternative 1: Tweak the Property Grid to show the nested Complex types. Given the Complexity of Property Grid and the time constraint to solve the problem, I had to go to the second alternative.

Alternative 2: After searching  lot on the internet, I stumbled here. The way to achieve the task is to bluff the Property Grid. Before you create the object, add the attributes to the properties of the type at runtime using COM (not Reflection). What you do is tell the Property Grid that the Type you used has the Attributes required for the former display the nested complex types.

The Code goes something like this:

AttributeCollection oldAttributes = TypeDescriptor.GetAttributes(type); 
Attribute[] newAttributes = new Attribute[oldAttributes.Count + 1]; 
oldAttributes.CopyTo(newAttributes, 0); 
newAttributes[oldAttributes.Count] = new TypeConverterAttribute(typeof(ExpandableObjectConverter)); 
TypeDescriptor.AddAttributes(type, newAttributes);

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Every application has a configuration file and you want to have some keys in that file which are

  • application specific
  • easily accessible in the application
  • have read and write operations

Read the keys with the following code:

System.Configuration.ConfigurationSettings.AppSettings["keyname"];

We can send WCF messages from a client application(application that consumes the WCF service) even without creating a proxy object.
For that, we can use the ChannelFactory.

If we have a contract called IMycontract and there is a message called MyMessage in the Contract with the channel IMyChannel, follow these steps to send the message without SVCUtil.exe.

ChannelFactory<IMyChannel> factory = new ChannelFactory<IMyChannel>(new BasicHttpBinding());
    IMyChannel channel = factory.CreateChannel(new EndpointAddress("http://10.10.10.10:2298/MyService/Service.svc"));
    channel.MyMessage (param1, param2);

You can include the headers as well. The IMyChannel is derieved from IMyContract and System.ServiceModel.IClientChannel.

I am working lately a lot on Reflection and WCF. So there will be quite a few posts on around those two.

This is one relates to the former category.

Problem: You want to create a Combo Box at runtime and the items to be included in the Combo Box should be read from an enum.

Solution: Read the Enumeration at runtime and fill the items of the combo box with them.

First get all the values from the Enumeration:

String[] names = System.Enum.GetNames(typeof(enumToRead));

enumToRead is the Enum which you have to read the names from.

Now, iterating through each of the names, add it to the Combo Box:

foreach (string name in lstProvider)
{
   cboxMyComboBox.Items.Add(name);
}

You can similarly get all the values in the enum by:

Array enumVal = Enum.GetValues(typeof(enumToRead)) ;

To retrieve the IP Address of the Machine on which the code is executed, you can use the following code:

 

string hostName = System.Net.Dns.GetHostName(); //get the name of the computer…

//gets the Ipaddress of the machine…

string IPAdd = System.Net.Dns.GetHostEntry(hostName ).AddressList[0].ToString();  

Next Page »