C# Captured Variable In Loop
Linq query built in foreach loop always takes parameter value from last iteration
Theoretical discussion, why this confusing behavior was implemented can be found in C#: Anonymous methods are not closures
/// Inspired by http://geekswithblogs.net/thibbard/archive/2007/05/04/Architecture-thoughts-and-Reflection.aspx
publicstaticbool CopyFleldsFromBaseClass(object from,object to)
bool rv = false;
if (from == null)
thrownewApplicationException("from is null");
if (to == null)
thrownewApplicationException("to is null");
System.Type typeFrom = from.GetType();
System.Type typeTo = from.GetType();
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public ;
//Debug.Assert(typeTo is typeFrom);
//iterate through each property of this
foreach (FieldInfo prop in typeFrom.GetFields(bindingFlags))
//get the name of the current property
string memberName = prop.Name;
//make sure this property is writeable and the obj property is readable
//if (prop.CanRead && to.GetType().GetFields(memberName).CanWrite)
if (typeTo.GetField(memberName, bindingFlags) != null)
//Get the value of this current property from obj
object objValue = typeFrom.GetField(memberName, bindingFlags).GetValue(from);
//Set the value of this property from obj
//if we are here, everything worked
rv = true;
catch (Exception exc)
//something bad happened, return false
rv = false;
By the way, my original idea was to create a derived class, when I have an object of base class.
Later I understood that it's better to create the derived object using factory at the start,
rather than create the base class and then convert it to derived one.
Related link on Rick Strahl's Blog Simplistic Object Copying in .NET
Similar Reflection approach to implement ToString is described here
declare @StartDate datetime
declare @EndDate datetime
declare @NotProcessedCount int
int = 10
Similar to the article Define custom error messages in SQL Server 2005
I’ve defined the error
EXEC sp_addmessage 60001, 1, N’Number of not-processed tasks %d exceed the limit on %s.’
ALTER PROCEDURE [dbo].[CheckNotProcessed] @limit int = 10 AS declare @StartDate datetime declare @EndDate datetime declare @NotProcessedCount int set @EndDate =GetDate()
I fould that this approach is very powerful and allow to send monitor regular business pricesses, as well as get notifications about some data conditions, that required investigation/debugging.
I wanted to replace some strings in files using my deployment MSbuild script.
I’ve noticed that MSBuild Community Tasks Project has RegexReplace task.
But when I’ve looked in documentation
(By the way, it will be good if Reference help will be available online, not only from download)
I’ve realized that the task is applicable for strings(e.g file names) not to content within a file.
Almost accidently in one of the posts i’ve found a reference to FileUpdate
task, that support Regex and does content replacements within a file.
The following examle (from downloaded help) search for a version number and update the revision.
<FileUpdate Files="version.txt" Regex="(d+).(d+).(d+).(d+)" ReplacementText="$1.$2.$3.123" />
Note: don’t forget to insert in to your project
You may require to change MSBuildCommunityTasksPath if it is not in current directory.