Testing a Comparer instance type

  • Thread starter Luc The Perverse
  • Start date
L

Luc The Perverse

Hello!

I am trying to find a way to deal with Windows' case insensitivity
without just forcing everything lowercase. While I am open to criticism
on my method - my question relates to the fourth to bottom line (not
including curly braces) on the bottom - testing to see if the Comparer
is not StringComparer.OrdinalIgnoreCase

My concern is eventually when I decide to serialize these to files, to
store persistant data, that somehow - either through usage,
implementation or design change that the type of the StringComparer will
get changed.

For simplicity sake, I have removed error handling and warnings.



public abstract class Task{ //A class to facilitate error handling and
progress for blocking calls
public Dictionary<string, Dictionary<string, string>> data;
protected abstract void execute();
public void ExecuteInBlockingCall(){
execute();
}
}

public class InitializeCaseInsensitiveFileStorage : Task { //prepare
dictionaries to hold case insensitive local files
protected override void execute() {
string[] alwaysDelete = {"new_dest", "new_hash", "new_date"};
string[] alwaysCheck = {"old_date", "old_hash", "old_dest"};
foreach(string i in alwaysDelete){
if(data.ContainsKey(i))
data.Remove(i);
data.Add(i, new Dictionary<string,
string>(StringComparer.OrdinalIgnoreCase));
}
foreach(string i in alwaysCheck)
if(!data.ContainsKey(i))
data.Add(i, new Dictionary<string,
string>(StringComparer.OrdinalIgnoreCase));
else if(data.Comparer!=StringComparer.OrdinalIgnoreCase){
data.Remove(i);
data.Add(i, new Dictionary<string,
string>(StringComparer.OrdinalIgnoreCase));
}
}


Is data.Comparer!=StringComparer.OrdinalIgnoreCase a reasonable way
to check and make sure that we are using the expected string comparer?
Will this work even after serialization? (I will add [Serializable]
tags and have this handled automatically)

Thank you
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

if(data.Comparer!=StringComparer.OrdinalIgnoreCase){


The above line might be incorrect, according to MSDN it returns an instance
of an anonymous class. It does not says if it will ALWAYS return the same
isntance.




Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
 
L

Luc The Perverse

Ignacio said:
Hi,

if(data.Comparer!=StringComparer.OrdinalIgnoreCase){


The above line might be incorrect, according to MSDN it returns an instance
of an anonymous class. It does not says if it will ALWAYS return the same
isntance.


Ok.

This is what I was worried about

Is there anyway I can test it short of adding something to the
dictionary and the making sure I find it again with capitalization
changed? I'm trying to avoud *that* method if possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top