Robert Wray: January 2008 Archives
I couldn't find any better, reusable way to get all the values from a specific column of a datatable, strongly typed than the snippet below:
internal class DataColumnConverter<T>
{
internal T[] GetValuesAsArray(DataTable data, string columnName)
{
List<T> values = new List<T>();
foreach (DataRow r in data.Rows)
{
values.Add((T)r[columnName]);
}
return values.ToArray();
}
}
Things it's missing:
- Type Cast checking on the type of items in the DataTables' column that's specified
- Checking that the column specified exists
- Handling of a column which contains nullable values (Could <T> be passed as, for example int? to cater for this? maybe! Or perhaps null's could be stripped out...)
- Truly Understanding Viewstate. Quite a long posting, with a lot of comments attached which also have some value. Well, well worth a read.
- Thoughts on the ASP.NET ViewState. More interesting content with a couple of useful/insightful comments.
- Master Page and PreInit. Not exactly entirely related to Viewstate, but it mentions a "trick" for getting access to controls on a page that has a Master Page associated, in the PreInit stage of the page lifecycle, something that otherwise blows a raspberry at the developer.

In this model you'd want to hide the web service as it may not ever be used (and really is an implementation detail!), the lack of an option to define the visibility of the Web Reference makes this impossible without hand editing the generated code.
Related Links
- The bug/feature request logged on MS Connect site. (Marked Closed/WontFix, grr!)
- Programatically adding Web References, I've not looked in too much detail, but maybe it's possible to use this to add an internal web reference?
A more recent entry mentions in passing someone's whining about the fact that there are a lot of ways to shutdown Windows Vista, particularly the length of the fly-out menu that gives the "advanced" options. Apparently it's too many to choose from. Given that by the time you get to that menu you already know (99% of the time2) what option you want, it's not so much a choice, is it? The same people who whinge about complexity are the ones that then whinge about things being "hidden" when simplification occurs. Grumble, grumble.
----
1 As with anything else on Wikipedia, this could be an utterly incorrect fiction that bears no actual similarity to Raymond Chen.
2 Yeah, a made-up statistic. Most of them are though? No? Just ask the British Government about their statistics ;)
In other news
- Useful guidelines for Hard Binding / String Freezing in .net from Microsofts P&P bods.
- Windows is not an MFC delivery channel:Feel free to substitute any other framework/DLL/library for "MFC".
Interestingly some comments are made regarding the fact that .net is delivered with Vista/Windows Server 2008, however, this is easily explained by the fact that system level components (IIS / IIS admin, anyone?) are built with .net. In fact, if you use the "Server Core" role, .net 3.0 isn't a part of the installed system, so it's not strictly true that Vista/2k8 are a delivery channel for .net, more that it's a dependency that some OS components have taken.
There are a couple of ways to handle getting at un-exposed methods for the purposes of Unit Testing, as described below. Method 2 is, I believe, pretty similar to the way that the Unit Testing tools in the higher editions of Visual Studio operate, albeit with the ability to automatically regenerate the code. Which do you prefer?
Method 1 – InternalsVisibleTo
Requires adding [assembly:
InternalsVisibleTo("CompanyName.xxxxxx.xxxxxx.Test")] to the AssemblyInfo.cs file for the CompanyName.xxxxxx.xxxxxx project, then declaring
any required methods/properties as internal rather than private.
Pros
-
Intellisense provided
by Visual Studio when writing Unit Tests
-
Immediate Compile
Time failures when attempting to build Unit Tests if the interface on class has
changed when Unit Tests are compiled
Cons
-
Other classes in the
same assembly have access to internal parts of a classes interface.
-
The runtime DLLs
contain a "reference" to the Test DLLs (albeit one that doesn’t do anything
unless the Test DLLs attempt to reference the runtime DLLs)
Method 2 – Reflection (Method
Invocation)
Requires using reflection, so for example to call the FacadeManager.Disconnect() method, the following code would be
required (can be shortcut)
Type t = FacadeManager.Instance.GetType();
MethodInfo Disconnect =
t.GetMethod("Disconnect",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
Object[] Parameters = new Object[]{(object)"Disconnecting
for Test _008_DisconnectMethodWorks"};
This can be wrapped in a testing support class, a la:
class FacadeManagerProxy
{
private FacadeManager
_facadeManagerInstance;
private Type _t;
private MethodInfo _disconnect;
private MethodInfo _connect;
public FacadeManagerProxy(FacadeManager facadeManagerInstance)
{
_facadeManagerInstance = facadeManagerInstance;
_t =
_facadeManagerInstance.GetType();
_disconnect = _t.GetMethod("Disconnect", BindingFlags.Instance
| BindingFlags.NonPublic);
_connect = _t.GetMethod("Connect", BindingFlags.Instance
| BindingFlags.NonPublic);
}
public void Disconnect(string
reason)
{
Object[]
Parameters = new Object[]
{ (object)reason };
_disconnect.Invoke(_facadeManagerInstance , Parameters);
}
public void Connect()
{
_connect.Invoke(_facadeManagerInstance, null);
}
}
For anyone experiencing similar difficulties, here's what has to be done, blog-by-blog:
(1) Export the blog using MT4's export feature.
(2) Copy the blog folder from my server to my computer as a backup, and because export doesn't retain any associated media files (videos, photos, podcasts) embedded within your blog posts.
(3) Delete the blog from within MT4.
(4) Delete the blog folder from my server, because deleting within MT4 apparently just deletes the blog from your database.
(5) Create a new blog, to be published in the same location as the old one.
(6) Transfer all of the media files BACK to the server (be sure you retain the same folder structure, or all your blog links will be broken).
(7) Import the blog entries and comments using MT4's import feature.
(8) Create all the custom changes that need to be made (new sidebars, widgets, etc).
(9) Assign an MT4 style theme to the new blog.
(10) Republish the blog from within MT4.
(11) Tweak the CSS to make the blog my own and not so cookie cutter.
