Mittwoch, 28. Oktober 2009
Der Codeausschnitt überprüft anhand eines Regex, ob ein übergebener String eine GUID ist. Es kann auch mit einem Convert innerhalb eines try-catch-Blocks gemacht werden, allerdings ist ein Regex schneller als Exceptions abarbeiten, was ebenfalls kein guter Programmierstil wäre…
private static Boolean IsGuid(String value)
{
if (String.IsNullOrEmpty(value))
{
return false;
}
return Regex.IsMatch(value, @"^?[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}?$", RegexOptions.IgnoreCase);
}
Thema: C# |
Beitrag kommentieren
Mittwoch, 30. September 2009
- Encapsulate what varies.
- Favor composition over inheritance.
- Program to interfaces, not implementations.
- Strive for loosely coupled design between objects that interact.
- Classes should be open for extension but closed for modification.
- Depend on abstractions. Do not depend on concrete classes.
- Only talk to your friends.
- Dont call us, we call you.
- A class should have only one reason to change.
Aus dem Buch “Head First Design Patterns” gesammelte Object Oriented Design Principles.
Thema: Design Patterns |
Beitrag kommentieren
Freitag, 21. August 2009
Heute hatte ich das Problem aus einer Collection ein DataSet zu erzeugen. Folgender Beitrag war dabei sehr hilfreich:
http://www.keithelder.net/blog/archive/2006/03/10/Converting-Generic-Lists-or-Collections-to-a-DataSet.aspx
Den dort vorgestellten Code, habe ich noch etwas verfeinert und leicht angepasst:
/// <summary>
/// This will take anything that implements the ICollection interface and convert
/// it to a DataSet.
/// </summary>
/// <example>
/// CollectiontoDataSet<CollectionType> converter = new CollectionToDataSet<CollectionType>(collection);
/// DataSet ds = converter.CreateDataSet("dataSetName", "dataTableName");
/// </example>
/// <typeparam name="T"></typeparam>
public class CollectionToDataSet<T> where T : System.Collections.ICollection
{
T _collection;
/// <summary>
/// Initializes a new instance of the <see cref="CollectionToDataSet<T>"/> class.
/// </summary>
/// <param name="list">The list.</param>
public CollectionToDataSet(T list)
{
_collection = list;
}
#region Properties
private string _dataTableName = String.Empty;
private PropertyInfo[] _propertyCollection = null;
private PropertyInfo[] PropertyCollection
{
get
{
if (_propertyCollection == null)
{
_propertyCollection = GetPropertyCollection();
}
return _propertyCollection;
}
}
#endregion
/// <summary>
/// Gets the property collection.
/// </summary>
/// <returns></returns>
private PropertyInfo[] GetPropertyCollection()
{
if (_collection.Count > 0)
{
IEnumerator enumerator = _collection.GetEnumerator();
enumerator.MoveNext();
return enumerator.Current.GetType().GetProperties();
}
return null;
}
/// <summary>
/// Fills the data table.
/// </summary>
/// <returns></returns>
private DataTable FillDataTable()
{
IEnumerator enumerator = _collection.GetEnumerator();
DataTable dt = CreateDataTable();
while (enumerator.MoveNext())
{
dt.Rows.Add(FillDataRow(dt.NewRow(), enumerator.Current));
}
return dt;
}
/// <summary>
/// Fills the data row.
/// </summary>
/// <param name="dataRow">The data row.</param>
/// <param name="p">The p.</param>
/// <returns></returns>
private DataRow FillDataRow(DataRow dataRow, object p)
{
foreach (PropertyInfo property in PropertyCollection)
{
dataRow[property.Name.ToString()] = property.GetValue(p, null);
}
return dataRow;
}
/// <summary>
/// Creates the data table.
/// </summary>
/// <returns></returns>
private DataTable CreateDataTable()
{
DataTable dt = new DataTable(_dataTableName);
foreach (PropertyInfo property in PropertyCollection)
{
dt.Columns.Add(property.Name.ToString());
}
return dt;
}
/// <summary>
/// Creates the data set.
/// </summary>
/// <param name="dataSetName">Name of the data set.</param>
/// <param name="dataTableName">Name of the data table.</param>
/// <returns>DataSet</returns>
public DataSet CreateDataSet(string dataSetName, string dataTableName)
{
_dataTableName = dataTableName;
DataSet ds = new DataSet(dataSetName);
ds.Tables.Add(FillDataTable());
return ds;
}
}
Thema: .NET > 3.0, C# |
Beitrag kommentieren
Dienstag, 11. August 2009
Ich zitiere aus dem Artikel http://dotnettipoftheday.org/tips/system_diagnostics_stopwatch.aspx:
“System.Diagnostics.Stopwatch is a replacement for what most people probably do to identify the time spent on excecuting a method. The process usually goes something like: create a DateTime.Now value at the start and then subtract the ending DateTime.Now to get the TimeDuration value that elapsed.
As an alternative, the Stopwatch class was built using low-level API calls, with less overhead than other .NET methods. If the hardware and Windows version of the computer support a high-resolution performance counter, it will use this counter instead of the standard PC clock.
Here is a simple example:”
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
//do my stuff
...
watch.Stop();
MessageBox.Show("Time spent: " + watch.Elapsed.ToString());
Thema: .NET > 3.0, C# |
Beitrag kommentieren
Donnerstag, 6. August 2009
Codesnippet zum encoden und decoden eines Strings in Base64:
/// <summary>
/// Encodes a string to Base64.
/// </summary>
/// <param name="toEncode">String to encode to Base64.</param>
/// <returns>Encoded string</returns>
public static string Base64Encode(string toEncode)
{
try
{
byte[] encodedString = Encoding.UTF8.GetBytes(toEncode);
return Convert.ToBase64String(encodedString);
}
catch (Exception e)
{
throw new Exception("Fehler beim Base64 Encoding. " + e.Message);
}
}
/// <summary>
/// Decodes the Base64 as string.
/// </summary>
/// <param name="toDecode">String to decode from Base64.</param>
/// <returns>Decoded string</returns>
public static string Base64Decode(string toDecode)
{
try
{
byte[] decodedString = Convert.FromBase64String(toDecode);
return Encoding.UTF8.GetString(decodedString);
}
catch (Exception e)
{
throw new Exception("Fehler beim Base64 Decoding." + e.Message);
}
}
Thema: .NET > 3.0, C# |
Beitrag kommentieren