Archive for the ‘C# 3.0’ Category
Posted by jpluimers on 2013/08/28
Every once in a while you are in situation where you are not allowed to use SQL Server Profiler, nor to see any query plans, but you still want see the SQL going from your .NET apps to the database server.
With that SQL, you can feed it through your favourite database tool, and see where the culprit is.
There are various ways of getting rudimentary or a bit more advanced SQL out of this. Flapper posted a solution that is specific for SQL Server (and requries both ObjectExtensions.cs and StringExtensions.cs from the DotNetX library), but posted more ready to use SQL.
I opted – and wanted to hank Justin Harris – for this piece of code on StackOverflow, which works on any IDbCommand (so you can use it for any ADO.NET data provider, like SQL Server, OLE DB, Oracle, etc):
While you will not be able to plug is into something like Enterprise Manager to run it works for logging.
public static string ToReadableString(this IDbCommand command)
{
StringBuilder builder = new StringBuilder();
if (command.CommandType == CommandType.StoredProcedure)
builder.AppendLine("Stored procedure: " + command.CommandText);
else
builder.AppendLine("Sql command: " + command.CommandText);
if (command.Parameters.Count > 0)
builder.AppendLine("With the following parameters.");
foreach (IDataParameter param in command.Parameters)
{
builder.AppendFormat(
" Paramater {0}: {1}",
param.ParameterName,
(param.Value == null ?
"NULL" : param.Value.ToString())).AppendLine();
}
return builder.ToString();
}
answered Apr 12 ’10 at 20:26; juharr
You saved my day! Not being allowed to use the profiler, this is a great way to get the actual SQL, then run it from SSMS or the Enterprise Manager. – Jeroen Wiert Pluimers
I pasted it in a DataExtensions class like this: Read the rest of this entry »
Posted in .NET, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »
Posted by jpluimers on 2013/08/08
Peter Leslie Morris asked if Delphi already incorporates the `yield` keyword that C# had introduced in C# 2.
Delphi doesn’t, but for the people interested what it does in C#:
Basically `yield` is syntactic sugar to make it a lot easier to write methods that return enumerators of some sort.
It delays (hence the yield keyword) execution until the enumerator as actually being used.
It is one of the hardest C# things to master (it is the most complicated transformation in the compiler, followed by anonymous methods – well maybe with the exception of async/await), but it can be very useful.
VB.NET doesn’t have it either (thanks André!) has it too, but and also has iterator blocks.
Some start posts on yield:
–jeroen
Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Delphi, Development, Software Development | 10 Comments »
Posted by jpluimers on 2013/08/06
Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 8.0, VB.NET 9.0, Visual Studio 11, Visual Studio 2010, Visual Studio and tools | 7 Comments »
Posted by jpluimers on 2013/08/03
If you ever get something similar to
InvalidCastException: [A]Person cannot be cast to [B]Person.
then you are (or more precise: your process is) probably loading the same assembly twice, but from a different context.
.NET allows that, and for reason (side by side loading, appdomains, etc).
Delphi doesn’t (thanks Warren Postma for noticing). But a mistake you see quite often is that Delphi developers try to load the same Delphi type from both an EXE and a DLL wondering why they don’t match. If you want to spread your types in Delphi, then use BPLs (run-time packages) for that. BPLs are similar to .NET assemblies, but cannot be loaded in memory twice.
–jeroen
via c# – InvalidCastException for two Objects of the same type – Stack Overflow.
Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 8.0, VB.NET 9.0, Visual Studio 11, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2013/07/25
Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Delphi, Delphi XE2, Delphi XE3, Development, Software Development | 4 Comments »
Posted by jpluimers on 2013/07/23
Ever since I started .NET programming after .NET Beta 1 Arrived in 2001, I found that many people struggle with the relation between assemblies and namespaces.
So I was glad that I posted this answer about 2.5 years ago on StackOverflow. Below is the slightly edited form:
People are easily confused by the namespace/assembly thing, as it decouples the concept of where your code is physically located (the assembly) and how you reference it:
- logically reference is by using the namespace
- physical reference is by referencing the assembly
I usually explain the relation using the word contribute:
- An assembly can contribute to multiple namespaces.
For instance, the System.Data.dll assembly contributes to namespaces like System.Data (e.g. the class System.Data.DataTable) and Microsoft.SqlServer.Server (e.g. the class Microsoft.SqlServer.Server.SqlContext).
- Multiple assemblies can contribute to a single namespace.
For instance both the System.Data.dll assembly and the System.Xml.dll assembly contribute to the System.Xml namespace.
Which means that if you use the System.Xml.XmlDataDocument class from your project, you need to reference the System.Data.dll assembly.
And if you use the System.Xml.XmlDocument class, you need to reference the System.Xml.dll from your project.
(the above examples are .NET 4.0, but likely hold for previous .NET versions as well).
Danny Thorpe explained the concept of namespace and internal really well, so I won’t go into detail about those.
Ever since I started .NET courses 10 years ago, I draw a table explaining assemblies and namespaces like this:
–jeroen
via: C# assemblies, whats in an assembly? – Stack Overflow.
Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, .NET CF, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »
Posted by jpluimers on 2013/07/18
A while ago, I wrote about .NET/C# duh moment of the day: “A char can be implicitly converted to ushort, int, uint, long, ulong, float, double, or decimal (not the other way around; implicit != implicit)”.
There is another duh moment having to do with the various C# operators like += which is being described as being
a += b
is equivalent to
a = a + b
You might think that this also holds:
a += b + c
is equivalent to
a = (a + b) + c
But Eric Lippert has explained this is not the case: it is equivalent to:
a = a + (b + c)
In his explanation, he also shows the confusion can get you very surprising results if you mix string, chars and ints in the expression: depending on the statement and ordering, you either concatenate characters, or add ints to characters.
He also recommends you should not do concatenation: either use String.Format, or StringBuilder. I totally agree with that.
Recommended reading!
–jeroen
Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »
Posted by jpluimers on 2013/07/11
Josh Stodola wrote a nice answer on the Determine .NET Framework version for dll – Stack Overflow question for using ILDASM to show the required .NET Framework/CLR version for an assembly.
From that, I wrote this tiny batch file:
ildasm.exe %1 /metadata[=MDHEADER] /text /noil | find "Metadata section:"
It gives output like this:
ildasm.exe C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.XML.dll /metadata[=MDHEADER] /text /noil | find "Metadata section:"
// Metadata section: 0x424a5342, version: 1.1, extra: 0, version len: 12, version: v4.0.30319
The cool thing is that older ILDASM versions work on assemblies requiring newer .NET Frameworks/CLRs.
So it is relatively future proof.
–jeroen
Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »
Posted by jpluimers on 2013/07/04
I’m not the only a big fan of almost anything that Eric Lippert writes on tech subjects on both his new personal Fabulous Adventures In Coding blog as well as his old microsoft Fabulous Adventures In Coding blog (a little more than half a year ago, he moved from Microsoft to Coverity.
On his old blog, he had a great series of posts on the “Fragile Base Class problem” (which he calls the Brittle Base Class problem) from a C# perspective.
Be sure to also read the comment threads, as they provide some very valuable information too (for instance, about Extension Methods that can make your code more fragile).
Recommended reading!
–jeroen
via: Browse by Tags – Fabulous Adventures In Coding – Site Home – MSDN Blogs.
Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »
Posted by jpluimers on 2013/06/27
log4net configuration can be a tad intimidating at first.
When you do not want to log all levels, and the levels you want to log are not noncontinuous, then you can combine the LevelMatchFilter with a DenyAllFilter.
The filter classes in the log4net do not provide much help on this, but the filters section in the log4net manual is better.
This StackOverflow question has a very nice answer explaining it: Discarding several log levels within a range with log4net.
–jeroen
via: Discarding several log levels within a range with log4net – Stack Overflow.
Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »