(How)
C^# You Are - 13 April 2008
This week's questions can be answered by using some unfamiliar, but available,
types in .NET.
- How can you periodically check for the existing of another machine on the
network? Answer
For such a task you generally ping the remote server periodically. Ping doesn't work in all cases since
it can be blocked but in general it is a good alternative.
To ping a remote machine you could use P/Invoke to call into the ICMP API of
Windows but there is an easier way. Visual Basic exposes a
My.Computer.Network class with some common functionality. One of these
functions is pinging a remote machine. Contrary to popular belief you can
invoke this method outside VB.
To use this class you need to do the following:
- Add a reference to the Microsoft.VisualBasic assembly. It is
available in the GAC.
- Add a using statement for the namespace Microsoft.VisualBasic.Devices.
- Create an instance of the Network class. It has no parameters.
- Call the Ping() method with the URL of the server to ping. For VB
users you can call the method directly by using My.Computer.Network.Ping.
- You need to copy a large byte array from one location to another. How can
you do it efficiently? Answer
The most efficient mechanism is to use the Buffer.BlockCopy method.
This static method allows you to copy an array of value types to another array
in an efficient manner. The only downside to this method is that the
offset you must use are in bytes rather than elements.
- Inside a class you maintain a collection of child objects. While the
parent class can manipulate the collection you do not want anyone else to be
able to. However you want to expose the children as a collection for
simplicity. How can you do this? Answer
The ReadOnlyCollection<T> class was created to represent a read-only
collection of objects. Both Array and List<T> expose
methods to convert the object to a read-only collection. To implement this
in the parent class you would create an instance of the class directly or call
the method to get the read-only collection and then return it.
The collection is only read-only in the sense that others cannot modify the list.
Technically ReadOnlyCollection<T> wraps the original collection. If
the original list is modified then the read-only version will reflect that
change. Therefore enumeration of a ReadOnlyCollection<T> is still
not thread-safe.
- You want to get the list of tables from a database. How can you do this? Answer
The GetSchema method exposed by DbConnection-derived classes can give you
access to a database's schema information including tables, columns, sprocs and
type system. The downside to this method is that it is very generalized to
the point where getting even something as simple as sprocs can be difficult
unless you know the provider in advance.
This is a complex method to get right. You should refer to online
documentation and trial and error to verify your information. I hope to
write an article on this topic, and add some support in Kraken, in the future.
- You are building the About box for your application and you'd like to retrieve
this information from the assembly data. How can you do it? Answer
The core assembly information such as product name, version, copyright and
trademark are merely attributes on the assembly. You can use the various
attribute classes (such as AssemblyProductAttribute or
AssemblyCopyrightAttribute).
An easier way though is to use the AssemblyInfo class. This class
effectively wraps these attributes into properties. It is part of the
Microsoft.VisualBasic assembly under the namespace
Microsoft.VisualBasic.ApplicationServices. Creating an instance of
this class just requires an assembly reference. You can get the EXE
assembly by calling Assembly.GetEntryAssembly().