<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Tobi + C# = T#</title> <link>http://www.fsmpi.uni-bayreuth.de/~dun3</link> <description>This is a collection of interesting stuff that I found on the web or stuff I worked on at the time.</description> <lastBuildDate>Tue, 22 Nov 2011 18:52:07 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/dun3" /><feedburner:info uri="dun3" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><title>Using conditional references to influence which assemblies are used for a specific project configuration or platform</title><link>http://feedproxy.google.com/~r/dun3/~3/Btq91z5xif0/496.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/using-conditional-references-to-influence-which-assemblies-are-used-for-a-specific-project-configuration-or-platform/496.html#comments</comments> <pubDate>Tue, 22 Nov 2011 18:52:07 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[Uncategorized]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=496</guid> <description><![CDATA[Introduction When using specific third party assemblies it is necessary to distinguish between platforms. Best example are e.g. log4net, sqlite, … which come in both x86 and x64 but not in the general Any Cpu flavor. A typical development scenario could be that an ASP.NET application is developed using the IIS Express and deployed on [...]]]></description> <content:encoded><![CDATA[<h3>Introduction</h3><p>When using specific third party assemblies it is necessary to distinguish between platforms. Best example are e.g. log4net, sqlite, … which come in both x86 and x64 but not in the general Any Cpu flavor. A typical development scenario could be that an ASP.NET application is developed using the IIS Express and deployed on a 64bit IIS in production.<br
/> A different development scenario could be that a Silverlight application uses Design Time data in order to create a richer design time experience for the developer. In this situation a best practice is to put the classes containing the design time data into a separate assembly that is only referenced during development and is not deployed in production. Here one needs to distinguish between solution configuration Debug and Release.<br
/> In order to support these scenarios the project files allow for conditional references.<br
/> Unfortunately Visual Studio does not expose the conditional references feature in its UI. Therefore one has to edit the corresponding .csproj file by hand. This can be simply done using Visual Studio using the following steps:</p><ol><li>Right click the project one wants to edit</li><li>Select "Unload Project" from the popup menu</li><li>Right click the unloaded project</li><li>Select "Edit <projectfilename>"</li></ol><h3>Basic project file layout (Visual Studio 2010)</h3><p>A project file (.csproj) is an XML file containing an XML header and a root element <em>Project</em>. The <em>Project</em> can contain multiple elements of different types, most notably the <em>PropertyGroup</em>, <em>ItemGroup</em>, <em>Import</em> and <em>Target</em> elements. In this post I will not describe the specific use of each of the elements, but will concentrate on conditional assemblies. Documentation for these elements can be found on msdn.<br
/> Assembly or project references are found in an ItemGroup using either <em>Reference</em> or <em>ProjectReference</em> elements.<br
/> A typical .csproj producing either an x86 or x64 executable looks like this:</p><div
class="igBar"><span
id="lxml-6"><a
href="#" onclick="javascript:showCodeTxt('xml-6'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-6"><div
class="xml"><ol><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;</span>?xml <span
class="re0">version</span>=<span
class="st0">"1.0"</span> <span
class="re0">encoding</span>=<span
class="st0">"utf-8"</span>?<span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2"><span
class="sc3"><span
class="re1">&lt;Project</span> <span
class="re0">ToolsVersion</span>=<span
class="st0">"4.0"</span> <span
class="re0">DefaultTargets</span>=<span
class="st0">"Build"</span> <span
class="re0">xmlns</span>=<span
class="st0">"http://schemas.microsoft.com/developer/msbuild/2003"</span><span
class="re2">&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Configuration</span> <span
class="re0">Condition</span>=<span
class="st0">" '$(Configuration)' == '' "</span><span
class="re2">&gt;</span></span>Debug<span
class="sc3"><span
class="re1">&lt;/Configuration<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Platform</span> <span
class="re0">Condition</span>=<span
class="st0">" '$(Platform)' == '' "</span><span
class="re2">&gt;</span></span>x86<span
class="sc3"><span
class="re1">&lt;/Platform<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ProductVersion<span
class="re2">&gt;</span></span></span>8.0.30703<span
class="sc3"><span
class="re1">&lt;/ProductVersion<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;SchemaVersion<span
class="re2">&gt;</span></span></span>2.0<span
class="sc3"><span
class="re1">&lt;/SchemaVersion<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ProjectGuid<span
class="re2">&gt;</span></span></span>{5793FBD7-6122-484B-AD86-AA211162D0EB}<span
class="sc3"><span
class="re1">&lt;/ProjectGuid<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;OutputType<span
class="re2">&gt;</span></span></span>Exe<span
class="sc3"><span
class="re1">&lt;/OutputType<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;AppDesignerFolder<span
class="re2">&gt;</span></span></span>Properties<span
class="sc3"><span
class="re1">&lt;/AppDesignerFolder<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;RootNamespace<span
class="re2">&gt;</span></span></span>Com.Hertkorn.DotNet.ConditionalAssemblyReferences<span
class="sc3"><span
class="re1">&lt;/RootNamespace<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;AssemblyName<span
class="re2">&gt;</span></span></span>Com.Hertkorn.DotNet.ConditionalAssemblyReferences<span
class="sc3"><span
class="re1">&lt;/AssemblyName<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;TargetFrameworkVersion<span
class="re2">&gt;</span></span></span>v4.0<span
class="sc3"><span
class="re1">&lt;/TargetFrameworkVersion<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;TargetFrameworkProfile<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;/TargetFrameworkProfile<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;FileAlignment<span
class="re2">&gt;</span></span></span>512<span
class="sc3"><span
class="re1">&lt;/FileAlignment<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;/PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;PropertyGroup</span> <span
class="re0">Condition</span>=<span
class="st0">" '$(Configuration)|$(Platform)' == 'Debug|x86' "</span><span
class="re2">&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;PlatformTarget<span
class="re2">&gt;</span></span></span>x86<span
class="sc3"><span
class="re1">&lt;/PlatformTarget<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugSymbols<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/DebugSymbols<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugType<span
class="re2">&gt;</span></span></span>full<span
class="sc3"><span
class="re1">&lt;/DebugType<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Optimize<span
class="re2">&gt;</span></span></span>false<span
class="sc3"><span
class="re1">&lt;/Optimize<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;OutputPath<span
class="re2">&gt;</span></span></span>bin\Debug\<span
class="sc3"><span
class="re1">&lt;/OutputPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DefineConstants<span
class="re2">&gt;</span></span></span>DEBUG;TRACE<span
class="sc3"><span
class="re1">&lt;/DefineConstants<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ErrorReport<span
class="re2">&gt;</span></span></span>prompt<span
class="sc3"><span
class="re1">&lt;/ErrorReport<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;WarningLevel<span
class="re2">&gt;</span></span></span>4<span
class="sc3"><span
class="re1">&lt;/WarningLevel<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;/PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;PropertyGroup</span> <span
class="re0">Condition</span>=<span
class="st0">" '$(Configuration)|$(Platform)' == 'Release|x86' "</span><span
class="re2">&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;PlatformTarget<span
class="re2">&gt;</span></span></span>x86<span
class="sc3"><span
class="re1">&lt;/PlatformTarget<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugType<span
class="re2">&gt;</span></span></span>pdbonly<span
class="sc3"><span
class="re1">&lt;/DebugType<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Optimize<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/Optimize<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;OutputPath<span
class="re2">&gt;</span></span></span>bin\Release\<span
class="sc3"><span
class="re1">&lt;/OutputPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DefineConstants<span
class="re2">&gt;</span></span></span>TRACE<span
class="sc3"><span
class="re1">&lt;/DefineConstants<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ErrorReport<span
class="re2">&gt;</span></span></span>prompt<span
class="sc3"><span
class="re1">&lt;/ErrorReport<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;WarningLevel<span
class="re2">&gt;</span></span></span>4<span
class="sc3"><span
class="re1">&lt;/WarningLevel<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;PropertyGroup</span> <span
class="re0">Condition</span>=<span
class="st0">"'$(Configuration)|$(Platform)' == 'Debug|x64'"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugSymbols<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/DebugSymbols<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;OutputPath<span
class="re2">&gt;</span></span></span>bin\x64\Debug\<span
class="sc3"><span
class="re1">&lt;/OutputPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DefineConstants<span
class="re2">&gt;</span></span></span>DEBUG;TRACE<span
class="sc3"><span
class="re1">&lt;/DefineConstants<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugType<span
class="re2">&gt;</span></span></span>full<span
class="sc3"><span
class="re1">&lt;/DebugType<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;PlatformTarget<span
class="re2">&gt;</span></span></span>x64<span
class="sc3"><span
class="re1">&lt;/PlatformTarget<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisLogFile<span
class="re2">&gt;</span></span></span>bin\Debug\Com.Hertkorn.DotNet.ConditionalAssemblyReferences.exe.CodeAnalysisLog.xml<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisLogFile<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisUseTypeNameInSuppression<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisUseTypeNameInSuppression<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisModuleSuppressionsFile<span
class="re2">&gt;</span></span></span>GlobalSuppressions.cs<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisModuleSuppressionsFile<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ErrorReport<span
class="re2">&gt;</span></span></span>prompt<span
class="sc3"><span
class="re1">&lt;/ErrorReport<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleSet<span
class="re2">&gt;</span></span></span>MinimumRecommendedRules.ruleset<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleSet<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleSetDirectories<span
class="re2">&gt;</span></span></span>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleSetDirectories<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisIgnoreBuiltInRuleSets<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisIgnoreBuiltInRuleSets<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleDirectories<span
class="re2">&gt;</span></span></span>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleDirectories<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisIgnoreBuiltInRules<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisIgnoreBuiltInRules<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;PropertyGroup</span> <span
class="re0">Condition</span>=<span
class="st0">"'$(Configuration)|$(Platform)' == 'Release|x64'"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;OutputPath<span
class="re2">&gt;</span></span></span>bin\x64\Release\<span
class="sc3"><span
class="re1">&lt;/OutputPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DefineConstants<span
class="re2">&gt;</span></span></span>TRACE<span
class="sc3"><span
class="re1">&lt;/DefineConstants<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Optimize<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/Optimize<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;DebugType<span
class="re2">&gt;</span></span></span>pdbonly<span
class="sc3"><span
class="re1">&lt;/DebugType<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;PlatformTarget<span
class="re2">&gt;</span></span></span>x64<span
class="sc3"><span
class="re1">&lt;/PlatformTarget<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisLogFile<span
class="re2">&gt;</span></span></span>bin\Release\Com.Hertkorn.DotNet.ConditionalAssemblyReferences.exe.CodeAnalysisLog.xml<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisLogFile<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisUseTypeNameInSuppression<span
class="re2">&gt;</span></span></span>true<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisUseTypeNameInSuppression<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisModuleSuppressionsFile<span
class="re2">&gt;</span></span></span>GlobalSuppressions.cs<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisModuleSuppressionsFile<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;ErrorReport<span
class="re2">&gt;</span></span></span>prompt<span
class="sc3"><span
class="re1">&lt;/ErrorReport<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleSet<span
class="re2">&gt;</span></span></span>MinimumRecommendedRules.ruleset<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleSet<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleSetDirectories<span
class="re2">&gt;</span></span></span>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleSetDirectories<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisIgnoreBuiltInRuleSets<span
class="re2">&gt;</span></span></span>false<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisIgnoreBuiltInRuleSets<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisRuleDirectories<span
class="re2">&gt;</span></span></span>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisRuleDirectories<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;CodeAnalysisIgnoreBuiltInRules<span
class="re2">&gt;</span></span></span>false<span
class="sc3"><span
class="re1">&lt;/CodeAnalysisIgnoreBuiltInRules<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/PropertyGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"CustomMarshalers"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Core"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Xml.Linq"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Data.DataSetExtensions"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"Microsoft.CSharp"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Data"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Xml"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Compile</span> <span
class="re0">Include</span>=<span
class="st0">"Program.cs"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;Compile</span> <span
class="re0">Include</span>=<span
class="st0">"Properties\AssemblyInfo.cs"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="sc3"><span
class="re1">&lt;None</span> <span
class="re0">Include</span>=<span
class="st0">"app.config"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;/ItemGroup<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;Import</span> <span
class="re0">Project</span>=<span
class="st0">"$(MSBuildToolsPath)\Microsoft.CSharp.targets"</span> <span
class="re2">/&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="coMULTI">&lt;!-- To modify your build process, add your task inside one of the targets below and uncomment it. </span></div></li><li
class="li2"><div
class="de2"><span
class="coMULTI">&nbsp; &nbsp; &nbsp; &nbsp;Other similar extension points exist, see Microsoft.Common.targets.</span></div></li><li
class="li1"><div
class="de1"><span
class="coMULTI">&nbsp; &lt;Target Name=&quot;BeforeBuild&quot;&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/Target<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;Target</span> <span
class="re0">Name</span>=<span
class="st0">"AfterBuild"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;/Target<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; --&gt;</div></li><li
class="li2"><div
class="de2"><span
class="sc3"><span
class="re1">&lt;/Project<span
class="re2">&gt;</span></span></span></div></li></ol></div></div></div><p></p><h3>Conditionals</h3><p>In the above project one can already spot the mechanism used to distinguish between e.g. configurations and platforms.</p><div
class="igBar"><span
id="lxml-7"><a
href="#" onclick="javascript:showCodeTxt('xml-7'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-7"><div
class="xml"><ol><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;PropertyGroup</span> <span
class="re0">Condition</span>=<span
class="st0">" '$(Configuration)|$(Platform)' == 'Release|x86' "</span><span
class="re2">&gt;</span></span></div></li></ol></div></div></div><p></p><p>An XML node annotated with the <em>Condition</em> attribute is only included when parsing the file if the condition holds true. This mechanism can be applied to any element in the project file's XML structure.</p><h3>Using conditions for assembly references</h3><p>If one uses an assembly that is specifically designed for x86 or x64 one can use the conditional attribute to include the correct dll for the specified build platform. One of the best known dll of that type is SQLite:</p><p>Without the conditional a dll reference looks like this:</p><div
class="igBar"><span
id="lxml-8"><a
href="#" onclick="javascript:showCodeTxt('xml-8'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-8"><div
class="xml"><ol><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Data.SQLite"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;HintPath<span
class="re2">&gt;</span></span></span>..\libs\sqlite\System.Data.SQLite.dll<span
class="sc3"><span
class="re1">&lt;/HintPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;/Reference<span
class="re2">&gt;</span></span></span></div></li></ol></div></div></div><p></p><p>To distinguish between the two states of 32bit and 64bit one simply doubles the<em>Reference</em> element and applies mutually exclusive <em>Condition</em> attributes to both:</p><div
class="igBar"><span
id="lxml-9"><a
href="#" onclick="javascript:showCodeTxt('xml-9'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-9"><div
class="xml"><ol><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Data.SQLite"</span> <span
class="re0">Condition</span>=<span
class="st0">"'$(PlatformTarget)' == 'x86'"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;HintPath<span
class="re2">&gt;</span></span></span>..\libs\sqlite\x86\System.Data.SQLite.dll<span
class="sc3"><span
class="re1">&lt;/HintPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;/Reference<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2"><span
class="sc3"><span
class="re1">&lt;Reference</span> <span
class="re0">Include</span>=<span
class="st0">"System.Data.SQLite"</span> <span
class="re0">Condition</span>=<span
class="st0">"'$(PlatformTarget)' == 'x64'"</span><span
class="re2">&gt;</span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;HintPath<span
class="re2">&gt;</span></span></span>..\libs\sqlite\x64\System.Data.SQLite.dll<span
class="sc3"><span
class="re1">&lt;/HintPath<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2"><span
class="sc3"><span
class="re1">&lt;/Reference<span
class="re2">&gt;</span></span></span></div></li></ol></div></div></div><p></p><p>Note that the HintPath differs depending on the platform.</p><h3>Using conditions for project references</h3><p>If you have a project that should not get deployed as part of the final released product, why not use the <em>Condition</em> attribute to only include the project during debugging. One good example for these kinds of projects is Design-time data. Simply distinguish on the <em>Configuration</em> setting.</p><div
class="igBar"><span
id="lxml-10"><a
href="#" onclick="javascript:showCodeTxt('xml-10'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-10"><div
class="xml"><ol><li
class="li1"><div
class="de1"><span
class="sc3"><span
class="re1">&lt;ProjectReference</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; <span
class="re0">Include</span>=<span
class="st0">"..\Com.Hertkorn.DotNet.ConditionalAssemblyReferences.Design\Com.Hertkorn.DotNet.ConditionalAssemblyReferences.Design.csproj"</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; <span
class="re0">Condition</span>=<span
class="st0">"'$(Configuration)' == 'Debug'"</span><span
class="re2">&gt;</span></span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="sc3"><span
class="re1">&lt;Project<span
class="re2">&gt;</span></span></span>{C7EAA6CA-1AF6-4D72-929B-D7D1177868D2}<span
class="sc3"><span
class="re1">&lt;/Project<span
class="re2">&gt;</span></span></span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="sc3"><span
class="re1">&lt;Name<span
class="re2">&gt;</span></span></span>Com.Hertkorn.DotNet.ConditionalAssemblyReferences.Design<span
class="sc3"><span
class="re1">&lt;/Name<span
class="re2">&gt;</span></span></span></div></li><li
class="li2"><div
class="de2"><span
class="sc3"><span
class="re1">&lt;/ProjectReference<span
class="re2">&gt;</span></span></span></div></li></ol></div></div></div><p></p><h3>Hint</h3><p><strong>Keep in mind</strong> that the Visual Studio UI does not reflect conditional references within the "References" folder of a project. Therefore all references are visible in any given configuration or platform selection. The compiler and Intellisense on the other hand are aware of conditional references, honoring the correct settings both with visual feedback and error notification during builds.</p><h3>Summary</h3><p>Today we looked at the possibility to customize which assemblies or projects are referenced in each individual configuration or platform selection. The customization cannot be done using the UI, but is accomplished by manually editing the project file.</p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=Btq91z5xif0:Aq-TiS0K1Do:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=Btq91z5xif0:Aq-TiS0K1Do:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/Btq91z5xif0" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/using-conditional-references-to-influence-which-assemblies-are-used-for-a-specific-project-configuration-or-platform/496.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/using-conditional-references-to-influence-which-assemblies-are-used-for-a-specific-project-configuration-or-platform/496.html</feedburner:origLink></item> <item><title>An updated look at Duck Typing in C# 4.0</title><link>http://feedproxy.google.com/~r/dun3/~3/OEv-GBQk6f8/490.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/updated-look-duck-typing-c-4-0-dynamic-dynamicobjec/490.html#comments</comments> <pubDate>Wed, 05 Jan 2011 20:29:19 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=490</guid> <description><![CDATA[Download the source: blogducktypingreloaded.zip It's time to do an update on my blog post from the end of 2008, when I did take a first look at the dynamic capabilities in .NET 4.0. From the beta to the final version of 4.0 they did unfortunatelly change the API used for dynamic dispatching and dynamic objects. [...]]]></description> <content:encoded><![CDATA[<p><em>Download the source: <a
href='http://www.fsmpi.uni-bayreuth.de/~dun3/tsharp/wp-content/uploads/2011/01/blogducktypingreloaded.zip'>blogducktypingreloaded.zip</a></em></p><p>It's time to do an update on my <a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/first-look-ducktyping-c-4-0-idynamicobject-metaobject/202.html">blog post from the end of 2008</a>, when I did take a first look at the dynamic capabilities in .NET 4.0. From the beta to the final version of 4.0 they did unfortunatelly change the API used for dynamic dispatching and dynamic objects. Instead of MetaObject and IDynamicObject all we need in the updated version of the API is System.Dynamics.DynamicObject. It takes the place of IDynamicObject and MetaObject is gone for good.</p><p>This makes it really pleasently easy to implement the same behaviour as I did years ago:</p><div
class="igBar"><span
id="lcsharp-13"><a
href="#" onclick="javascript:showCodeTxt('csharp-13'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-13"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">public</span> <span
class="kw4">class</span> Duck : DynamicObject</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">private</span> Dictionary&lt;string, string&gt; dictionary = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> Dictionary&lt;string, string&gt;<span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">public</span> <span
class="kw1">override</span> <span
class="kw4">bool</span> TryGetMember<span
class="br0">&#40;</span>GetMemberBinder binder, <span
class="kw1">out</span> <span
class="kw4">object</span> result<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw4">string</span> resultString;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; var didWork = TryGetMember<span
class="br0">&#40;</span>binder.<span
class="me1">Name</span>, <span
class="kw1">out</span> resultString<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; result = resultString;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> didWork;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">private</span> <span
class="kw4">bool</span> TryGetMember<span
class="br0">&#40;</span><span
class="kw4">string</span> name, <span
class="kw1">out</span> <span
class="kw4">string</span> result<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> dictionary.<span
class="me1">TryGetValue</span><span
class="br0">&#40;</span>name, <span
class="kw1">out</span> result<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">public</span> <span
class="kw1">override</span> <span
class="kw4">bool</span> TrySetMember<span
class="br0">&#40;</span>SetMemberBinder binder, <span
class="kw4">object</span> value<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> TrySetMember<span
class="br0">&#40;</span>binder.<span
class="me1">Name</span>, value<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">private</span> <span
class="kw4">bool</span> TrySetMember<span
class="br0">&#40;</span><span
class="kw4">string</span> name, <span
class="kw4">object</span> value<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">try</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dictionary<span
class="br0">&#91;</span>name<span
class="br0">&#93;</span> = value == <span
class="kw1">null</span> ? <span
class="kw1">null</span> : value.<span
class="me1">ToString</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> <span
class="kw1">true</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">catch</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> <span
class="kw1">false</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">public</span> <span
class="kw1">override</span> <span
class="kw4">bool</span> TryConvert<span
class="br0">&#40;</span>ConvertBinder binder, <span
class="kw1">out</span> <span
class="kw4">object</span> result<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">try</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = Generator.<span
class="me1">GenerateProxy</span><span
class="br0">&#40;</span>binder.<span
class="me1">Type</span>, <span
class="kw1">this</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> <span
class="kw1">true</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">catch</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = <span
class="kw1">null</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">return</span> <span
class="kw1">false</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>This is a dynamic Duck that can store any value when given via Property and will return the same value as a string when asked for it via the dynamic keyword.</p><div
class="igBar"><span
id="lcsharp-14"><a
href="#" onclick="javascript:showCodeTxt('csharp-14'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-14"><div
class="csharp"><ol><li
class="li1"><div
class="de1">dynamic d = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> Duck<span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">d.<span
class="me1">Test</span> = i;</div></li><li
class="li1"><div
class="de1">dynamic a = d.<span
class="me1">Test</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">Console.<span
class="me1">WriteLine</span><span
class="br0">&#40;</span>a<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">Console.<span
class="me1">WriteLine</span><span
class="br0">&#40;</span>a.<span
class="me1">GetType</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>.<span
class="me1">FullName</span> + <span
class="st0">" (Should be System.String)"</span><span
class="br0">&#41;</span>;</div></li></ol></div></div></div><p></p><p>When you download the sample you can see that the code also includes the ability to do a cast from the dynamic Duck to any given interface, in this case an IQuack. So now the totally dynamic property bag is usable via a strongly typed interface!</p><p><em>Download the source: <a
href='http://www.fsmpi.uni-bayreuth.de/~dun3/tsharp/wp-content/uploads/2011/01/blogducktypingreloaded.zip'>blogducktypingreloaded.zip</a></em></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=OEv-GBQk6f8:gvqFU9WyiWk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=OEv-GBQk6f8:gvqFU9WyiWk:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/OEv-GBQk6f8" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/updated-look-duck-typing-c-4-0-dynamic-dynamicobjec/490.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/updated-look-duck-typing-c-4-0-dynamic-dynamicobjec/490.html</feedburner:origLink></item> <item><title>Creating an improved ASP.NET MVC 3 Html.ImageActionLink</title><link>http://feedproxy.google.com/~r/dun3/~3/mD9C0U6uhG0/483.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html#comments</comments> <pubDate>Sun, 05 Dec 2010 08:20:06 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=483</guid> <description><![CDATA[The old version of Html.ImageActionLink had a serious flaw - I had to resolve to replacing parts of the generated link after the fact in order to get non html encoded information in there. That obviously creates, though not really probable in this specific case, the problem of unwanted replacement is possible. So I learnt [...]]]></description> <content:encoded><![CDATA[<p><a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/creating-an-asp-net-mvc-3-html-actionlink-that-is-an-image/479.html">The old version of Html.ImageActionLink</a> had a serious flaw - I had to resolve to replacing parts of the generated link after the fact in order to get non html encoded information in there. That obviously creates, though not really probable in this specific case, the problem of unwanted replacement is possible.</p><p>So I learnt about UrlHelper and generate the whole MvcHtmlString in one go, without falling back on replacing stuff.</p><div
class="igBar"><span
id="lcsharp-17"><a
href="#" onclick="javascript:showCodeTxt('csharp-17'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-17"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">public</span> <span
class="kw1">static</span> MvcHtmlString ImageActionLink<span
class="br0">&#40;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">this</span> HtmlHelper helper,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">string</span> imageUrl,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">string</span> altText,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">string</span> actionName,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">string</span> controllerName,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">object</span> routeValues,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">object</span> linkHtmlAttributes,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">object</span> imgHtmlAttributes<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; var linkAttributes = AnonymousObjectToKeyValue<span
class="br0">&#40;</span>linkHtmlAttributes<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var imgAttributes = AnonymousObjectToKeyValue<span
class="br0">&#40;</span>imgHtmlAttributes<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var imgBuilder = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> TagBuilder<span
class="br0">&#40;</span><span
class="st0">"img"</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; imgBuilder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span><span
class="st0">"src"</span>, imageUrl<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; imgBuilder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span><span
class="st0">"alt"</span>, altText<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; imgBuilder.<span
class="me1">MergeAttributes</span><span
class="br0">&#40;</span>imgAttributes, <span
class="kw1">true</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var urlHelper = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> UrlHelper<span
class="br0">&#40;</span>helper.<span
class="me1">ViewContext</span>.<span
class="me1">RequestContext</span>, helper.<span
class="me1">RouteCollection</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var linkBuilder = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> TagBuilder<span
class="br0">&#40;</span><span
class="st0">"a"</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; linkBuilder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span><span
class="st0">"href"</span>, urlHelper.<span
class="me1">Action</span><span
class="br0">&#40;</span>actionName, controllerName, routeValues<span
class="br0">&#41;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; linkBuilder.<span
class="me1">MergeAttributes</span><span
class="br0">&#40;</span>linkAttributes, <span
class="kw1">true</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; var text = linkBuilder.<span
class="me1">ToString</span><span
class="br0">&#40;</span>TagRenderMode.<span
class="me1">StartTag</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; text += imgBuilder.<span
class="me1">ToString</span><span
class="br0">&#40;</span>TagRenderMode.<span
class="me1">SelfClosing</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; text += linkBuilder.<span
class="me1">ToString</span><span
class="br0">&#40;</span>TagRenderMode.<span
class="me1">EndTag</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">return</span> MvcHtmlString.<span
class="me1">Create</span><span
class="br0">&#40;</span>text<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2"><span
class="kw1">private</span> <span
class="kw1">static</span> Dictionary&lt;string, object&gt; AnonymousObjectToKeyValue<span
class="br0">&#40;</span><span
class="kw4">object</span> anonymousObject<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var dictionary = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> Dictionary&lt;string, object&gt;<span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>anonymousObject != <span
class="kw1">null</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">foreach</span> <span
class="br0">&#40;</span>PropertyDescriptor propertyDescriptor <span
class="kw1">in</span> TypeDescriptor.<span
class="me1">GetProperties</span><span
class="br0">&#40;</span>anonymousObject<span
class="br0">&#41;</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dictionary.<span
class="me1">Add</span><span
class="br0">&#40;</span>propertyDescriptor.<span
class="me1">Name</span>, propertyDescriptor.<span
class="me1">GetValue</span><span
class="br0">&#40;</span>anonymousObject<span
class="br0">&#41;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">return</span> dictionary;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>The interface of the method is exactly the same so I can still use this version to create an image that is an actionlink in razor like this:</p><div
class="igBar"><span
id="lxml-18"><a
href="#" onclick="javascript:showCodeTxt('xml-18'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-18"><div
class="xml"><ol><li
class="li1"><div
class="de1">@Html.ImageActionLink(</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;Url.Content(&quot;~/Content/Images/&quot; + url),</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;alt,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;&quot;Details&quot;,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;&quot;Calender&quot;,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;new { area = &quot;&quot;, day = Model.Date.Day, month = Model.Date.Month, year = Model.Date.Year },</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;new { @class = &quot;SelectDay&quot;, onClick = string.Format(&quot;DoCallbackBookingDetails({0},{1},{2}); return false;&quot;, Model.Date.Day, Model.Date.Month, Model.Date.Year) },</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;new { style = &quot;border: 0px;&quot; })</div></li></ol></div></div></div><p></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=mD9C0U6uhG0:aPlvRDIzh9o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=mD9C0U6uhG0:aPlvRDIzh9o:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/mD9C0U6uhG0" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html/feed</wfw:commentRss> <slash:comments>10</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html</feedburner:origLink></item> <item><title>Creating an ASP.NET MVC 3 Html.ActionLink that is an Image</title><link>http://feedproxy.google.com/~r/dun3/~3/rukZ3MCqZ5g/479.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/creating-an-asp-net-mvc-3-html-actionlink-that-is-an-image/479.html#comments</comments> <pubDate>Sat, 04 Dec 2010 22:28:55 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://www.fsmpi.uni-bayreuth.de/~dun3/?p=479</guid> <description><![CDATA[[UPDATE: There is an improved version of Html.ActionLink: Creating an improved ASP.NET MVC 3 Html.ImageActionLink] Have I told you that I really enjoy ASP.NET MVC 3 Razor? Damn, that's nice to work with. I am doing a small, fun project for a friend of mine and I stumbled across the situation that I wanted to [...]]]></description> <content:encoded><![CDATA[<p><em>[UPDATE: There is an improved version of Html.ActionLink: <a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html">Creating an improved ASP.NET MVC 3 Html.ImageActionLink</a>]</em></p><p>Have I told you that I really enjoy ASP.NET MVC 3 Razor? Damn, that's nice to work with. I am doing a small, fun project for a friend of mine and I stumbled across the situation that I wanted to have an image as an ActionLink. And since the @Html.ActionLink syntax is really sexy, I wanted to write an Extension method that does the same with an image.</p><p>The solution is this quick hack:</p><p><em>[UPDATE: There is an improved version without the hacky replace: <a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html">Creating an improved ASP.NET MVC 3 Html.ImageActionLink</a>]</em></p><div
class="igBar"><span
id="lcsharp-21"><a
href="#" onclick="javascript:showCodeTxt('csharp-21'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-21"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">public</span> <span
class="kw1">static</span> MvcHtmlString ImageActionLink<span
class="br0">&#40;</span><span
class="kw1">this</span> HtmlHelper helper,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">string</span> imageUrl,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">string</span> altText,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">string</span> actionName,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">string</span> controllerName,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">object</span> routeValues,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw4">object</span> linkHtmlAttributes,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw4">object</span> imgHtmlAttributes<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var attributes = HtmlHelper.<span
class="me1">AnonymousObjectToHtmlAttributes</span><span
class="br0">&#40;</span>imgHtmlAttributes<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var builder = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> TagBuilder<span
class="br0">&#40;</span><span
class="st0">"img"</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; builder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span><span
class="st0">"src"</span>, imageUrl<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; builder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span><span
class="st0">"alt"</span>, altText<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">foreach</span> <span
class="br0">&#40;</span>var key <span
class="kw1">in</span> attributes.<span
class="me1">Keys</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; var value = attributes<span
class="br0">&#91;</span>key<span
class="br0">&#93;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw4">string</span> valueAsString = <span
class="kw1">null</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>value != <span
class="kw1">null</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valueAsString = value.<span
class="me1">ToString</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; &nbsp; builder.<span
class="me1">MergeAttribute</span><span
class="br0">&#40;</span>key, valueAsString<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; var link = helper.<span
class="me1">ActionLink</span><span
class="br0">&#40;</span><span
class="st0">"[placeholder]"</span>, actionName, controllerName, routeValues, linkHtmlAttributes<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; var text = link.<span
class="me1">ToHtmlString</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; text = text.<span
class="me1">Replace</span><span
class="br0">&#40;</span><span
class="st0">"[placeholder]"</span>, builder.<span
class="me1">ToString</span><span
class="br0">&#40;</span>TagRenderMode.<span
class="me1">SelfClosing</span><span
class="br0">&#41;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">return</span> MvcHtmlString.<span
class="me1">Create</span><span
class="br0">&#40;</span>text<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>We return a MvcHtmlString, so we can indicate that this sting is not supposed to be html encoded after we return it to the templating engine. Plus we use the handy AnonymousObjectToHtmlAttributes helper method in order to enable us to pass html attributes to the img tag using anonymous classes, just as we are used to by now.</p><p>Unfortunatelly we can not tell the helper.ActionLink call to not html encode what we pass as linkText - therefore we fall back on using a place holder that we replace after the fact.</p><p>So now we can do a</p><div
class="igBar"><span
id="lxml-22"><a
href="#" onclick="javascript:showCodeTxt('xml-22'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">XML:</span><div
id="xml-22"><div
class="xml"><ol><li
class="li1"><div
class="de1">@Html.ImageActionLink(</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;Url.Content(&quot;~/Content/Images/&quot; + url),</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;alt,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;&quot;Details&quot;,</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;&quot;Calender&quot;,</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;new { area = &quot;&quot;, day = Model.Date.Day, month = Model.Date.Month, year = Model.Date.Year },</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;new { @class = &quot;SelectDay&quot;, onClick = string.Format(&quot;DoCallbackBookingDetails({0},{1},{2}); return false;&quot;, Model.Date.Day, Model.Date.Month, Model.Date.Year) },</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp;new { style = &quot;border: 0px;&quot; })</div></li></ol></div></div></div><p></p><p>right inside Razor. Beautiful!</p><p><em>[UPDATE: There is an improved helper to create an image that has an actionlink url a href: <a
href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/asp-net-mvc-3-html-actionlink-image-imageactionlink-improved/483.html">Creating an improved ASP.NET MVC 3 Html.ImageActionLink</a>]</em></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=rukZ3MCqZ5g:oFOyNdYTBc0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=rukZ3MCqZ5g:oFOyNdYTBc0:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/rukZ3MCqZ5g" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/creating-an-asp-net-mvc-3-html-actionlink-that-is-an-image/479.html/feed</wfw:commentRss> <slash:comments>2</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/creating-an-asp-net-mvc-3-html-actionlink-that-is-an-image/479.html</feedburner:origLink></item> <item><title>Active Directory Tip #9 – How to escape the Path property</title><link>http://feedproxy.google.com/~r/dun3/~3/svp6hfci8DE/458.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-9-escape-path-property-80005000/458.html#comments</comments> <pubDate>Mon, 17 May 2010 19:00:28 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[Active Directory]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=458</guid> <description><![CDATA[Did you get the dreaded 0x80005000 COMException "Unknown error"? Maybe you should check, if your AD entry contains a forward slash '/' in its DN. Because there is a subtile difference between a correctly escaped distinguished name and a correctly escaped ldap path. Observe: PLAIN TEXT PLAIN: Unescaped:&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; [...]]]></description> <content:encoded><![CDATA[<p>Did you get the dreaded 0x80005000 COMException "Unknown error"? Maybe you should check, if your AD entry contains a forward slash '/' in its DN. Because there is a subtile difference between a correctly escaped distinguished name and a correctly escaped ldap path.</p><p>Observe:</p><div
class="igBar"><span
id="lplain-25"><a
href="#" onclick="javascript:showCodeTxt('plain-25'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PLAIN:</span><div
id="plain-25"><div
class="plain"><ol><li
class="li1"><div
class="de1">Unescaped:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CN=Test\ Me \\ huhu \/ cool / \\/ <span
class="co1">// \\// \\\/// \\\\////! /////,OU=AdTests,DC=test,DC=domain,DC=local</span></div></li><li
class="li2"><div
class="de2">Escaped DN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CN=Test\\ Me \\\\ huhu \\/ cool / \\\\/ <span
class="co1">// \\\\// \\\\\\/// \\\\\\\\////! /////,OU=AdTests,DC=test,DC=domain,DC=local</span></div></li><li
class="li1"><div
class="de1">Escaped Path: LDAP:<span
class="co1">//test.domain.local:389/CN=Test\\ Me \\\\ huhu \\\/ cool \/ \\\\\/ \/\/ \\\\\/\/ \\\\\\\/\/\/ \\\\\\\\\/\/\/\/! \/\/\/\/\/,OU=AdTests,DC=test,DC=domain,DC=local </span></div></li></ol></div></div></div><p></p><p>Notice how forward slashes need to be escaped in addition to how distinguished names get escaped. This is a very subtile thing that is often overlooked. So when converting between DNs and Paths you have to use the following methods:</p><div
class="igBar"><span
id="lcsharp-26"><a
href="#" onclick="javascript:showCodeTxt('csharp-26'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-26"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">private</span> <span
class="kw1">static</span> <span
class="kw1">readonly</span> <span
class="kw4">string</span> LDAP_PREFIX = <span
class="st0">@"LDAP://test.domain.local:389/"</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1"><span
class="kw1">private</span> <span
class="kw1">static</span> <span
class="kw4">string</span> ConvertDnToPath<span
class="br0">&#40;</span><span
class="kw4">string</span> dn<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; var exploded = dn.<span
class="me1">ToCharArray</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; StringBuilder sb = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> StringBuilder<span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">foreach</span> <span
class="br0">&#40;</span>var @<span
class="kw4">char</span> <span
class="kw1">in</span> exploded<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>@<span
class="kw4">char</span> == <span
class="st0">'/'</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; sb.<span
class="me1">Append</span><span
class="br0">&#40;</span><span
class="st0">'<span
class="es0">\\</span>'</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; sb.<span
class="me1">Append</span><span
class="br0">&#40;</span>@<span
class="kw4">char</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw1">return</span> LDAP_PREFIX + sb.<span
class="me1">ToString</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2"><span
class="kw1">private</span> <span
class="kw1">static</span> <span
class="kw4">string</span> ConvertPathToDn<span
class="br0">&#40;</span><span
class="kw4">string</span> path<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; path = path.<span
class="me1">Substring</span><span
class="br0">&#40;</span>path.<span
class="me1">IndexOf</span><span
class="br0">&#40;</span><span
class="st0">'/'</span>, <span
class="st0">"LDAP://"</span>.<span
class="me1">Length</span><span
class="br0">&#41;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="co1">// remove leading /</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">while</span> <span
class="br0">&#40;</span>path.<span
class="me1">StartsWith</span><span
class="br0">&#40;</span><span
class="st0">"/"</span><span
class="br0">&#41;</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; path = path.<span
class="me1">Substring</span><span
class="br0">&#40;</span><span
class="nu0">1</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>!path.<span
class="me1">Contains</span><span
class="br0">&#40;</span><span
class="st0">'/'</span><span
class="br0">&#41;</span><span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="kw1">return</span> path;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; StringBuilder sb = <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> StringBuilder<span
class="br0">&#40;</span>path<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw4">bool</span> slashMode = <span
class="kw1">false</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw4">int</span> backslashCount = <span
class="nu0">0</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw1">for</span> <span
class="br0">&#40;</span><span
class="kw4">int</span> i = path.<span
class="me1">Length</span> - <span
class="nu0">1</span>; i&gt;= <span
class="nu0">0</span>; i--<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>path<span
class="br0">&#91;</span>i<span
class="br0">&#93;</span> == <span
class="st0">'<span
class="es0">\\</span>'</span> &amp;&amp; slashMode<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; backslashCount++;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">else</span> <span
class="kw1">if</span> <span
class="br0">&#40;</span>slashMode<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span><span
class="br0">&#40;</span>backslashCount % <span
class="nu0">2</span><span
class="br0">&#41;</span> != <span
class="nu0">0</span><span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; &nbsp; sb.<span
class="me1">Remove</span><span
class="br0">&#40;</span>i + <span
class="nu0">1</span>, <span
class="nu0">1</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; backslashCount = <span
class="nu0">0</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; slashMode = <span
class="kw1">false</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>path<span
class="br0">&#91;</span>i<span
class="br0">&#93;</span> == <span
class="st0">'/'</span><span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; <span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; &nbsp; backslashCount = <span
class="nu0">0</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; &nbsp; slashMode = <span
class="kw1">true</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">return</span> sb.<span
class="me1">ToString</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=svp6hfci8DE:7Viw5Ww3tSw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=svp6hfci8DE:7Viw5Ww3tSw:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/svp6hfci8DE" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-9-escape-path-property-80005000/458.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-9-escape-path-property-80005000/458.html</feedburner:origLink></item> <item><title>Active Directory Tip #8 – Set Password of user</title><link>http://feedproxy.google.com/~r/dun3/~3/AO_RXZfCrTM/453.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-8-set-password-of-user-person/453.html#comments</comments> <pubDate>Fri, 14 May 2010 18:21:26 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[Active Directory]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=453</guid> <description><![CDATA[The password of a person entry is not a regular property with direct access for reading and writing. Instead it can't be read just indirectly written to via the setPassword method. PLAIN TEXT C#: string password = "secret"; directoryEntry.Invoke&#40;"setPassword", password&#41;; directoryEntry.RefreshCache&#40;&#41;; The caller must have the User-Force-Change-Password Extended Right to set the password with this [...]]]></description> <content:encoded><![CDATA[<p>The password of a person entry is not a regular property with direct access for reading and writing. Instead it can't be read just indirectly written to via the setPassword method.</p><div
class="igBar"><span
id="lcsharp-28"><a
href="#" onclick="javascript:showCodeTxt('csharp-28'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-28"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw4">string</span> password = <span
class="st0">"secret"</span>;</div></li><li
class="li2"><div
class="de2">directoryEntry.<span
class="me1">Invoke</span><span
class="br0">&#40;</span><span
class="st0">"setPassword"</span>, password<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">directoryEntry.<span
class="me1">RefreshCache</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li></ol></div></div></div><p></p><p>The caller must have the <a
href="http://msdn.microsoft.com/en-us/library/ms684414.aspx">User-Force-Change-Password Extended Right</a> to set the password with this method.</p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=AO_RXZfCrTM:on6WE4L7YHM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=AO_RXZfCrTM:on6WE4L7YHM:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/AO_RXZfCrTM" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-8-set-password-of-user-person/453.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-8-set-password-of-user-person/453.html</feedburner:origLink></item> <item><title>Active Directory Tip #7 – Handling a Bit Property (e.g. userAccountControl – Account is disabled)</title><link>http://feedproxy.google.com/~r/dun3/~3/DCPBjTSyXoo/450.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-7-handling-bit-property-useraccountcontrol-account-disable/450.html#comments</comments> <pubDate>Wed, 12 May 2010 18:14:05 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[Active Directory]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=450</guid> <description><![CDATA[Bit properties like User-Account-Control Attribute in the Active Directory are represented by regular integers. Therefore all the usual bit operation may be performed on those fields. PLAIN TEXT C#: public static bool GetBitAttribute&#40;DirectoryEntry directoryEntry, string attributeName, int bitmask&#41; &#123; &#160; int value = &#40;int&#41;DirectoryEntryHelper.GetAdObjectProperty&#40;directoryEntry, attributeName&#41;; &#160; bool result = &#40;bitmask &#38; value&#41; == bitmask; &#160; [...]]]></description> <content:encoded><![CDATA[<p>Bit properties like <a
href="http://msdn.microsoft.com/en-us/library/ms680832.aspx">User-Account-Control Attribute</a> in the Active Directory are represented by regular integers. Therefore all the usual bit operation may be performed on those fields.</p><div
class="igBar"><span
id="lcsharp-32"><a
href="#" onclick="javascript:showCodeTxt('csharp-32'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-32"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">public</span> <span
class="kw1">static</span> <span
class="kw4">bool</span> GetBitAttribute<span
class="br0">&#40;</span>DirectoryEntry directoryEntry, <span
class="kw4">string</span> attributeName, <span
class="kw4">int</span> bitmask<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw4">int</span> value = <span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span>DirectoryEntryHelper.<span
class="me1">GetAdObjectProperty</span><span
class="br0">&#40;</span>directoryEntry, attributeName<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw4">bool</span> result = <span
class="br0">&#40;</span>bitmask &amp; value<span
class="br0">&#41;</span> == bitmask;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw1">return</span> result;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2"><span
class="kw1">public</span> <span
class="kw1">static</span> <span
class="kw1">void</span> SetBitAttribute<span
class="br0">&#40;</span>DirectoryEntry directoryEntry, <span
class="kw4">string</span> attributeName, <span
class="kw4">int</span> bitmask, <span
class="kw4">bool</span> attributeValue<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; PropertyValueCollection property = directoryEntry.<span
class="me1">Properties</span><span
class="br0">&#91;</span>attributeName<span
class="br0">&#93;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw4">int</span> value = <span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span>property.<span
class="me1">Value</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">if</span> <span
class="br0">&#40;</span>attributeValue<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; value |= bitmask;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">else</span></div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; &nbsp; value &amp;= ~bitmask;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; property.<span
class="me1">Value</span> = value;</div></li><li
class="li2"><div
class="de2"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>With these helper functions deactivating a user would be accomplished by using</p><div
class="igBar"><span
id="lcsharp-33"><a
href="#" onclick="javascript:showCodeTxt('csharp-33'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-33"><div
class="csharp"><ol><li
class="li1"><div
class="de1">SetBitAttribute<span
class="br0">&#40;</span>directoryEntry, <span
class="st0">"userAccountControl"</span>, ADS_UF_ACCOUNTDISABLE, <span
class="kw1">true</span><span
class="br0">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Just for convenience, here is the table form <a
href="http://msdn.microsoft.com/en-us/library/ms680832.aspx">User-Account-Control Attribute</a> converted to C# syntax:</p><div
class="igBar"><span
id="lcsharp-34"><a
href="#" onclick="javascript:showCodeTxt('csharp-34'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-34"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The logon script is executed.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_SCRIPT = 0x00000001;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The user account is disabled.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_ACCOUNTDISABLE = 0x00000002;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The home directory is required.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_HOMEDIR_REQUIRED = 0x00000008;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The account is currently locked out.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_LOCKOUT = 0x00000010;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// No password is required.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_PASSWD_NOTREQD = 0x00000020;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The user cannot change the password.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_PASSWD_CANT_CHANGE = 0x00000040;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The user can send an encrypted password.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0x00000080;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// This is an account for users whose primary account is in </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// another domain. This account provides user access to this </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// domain, but not to any domain that trusts this domain. </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// Also known as a local user account.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_TEMP_DUPLICATE_ACCOUNT = 0x00000100;</div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// This is a default account type that represents a typical user.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_NORMAL_ACCOUNT = 0x00000200;</div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// This is a permit to trust account for a system domain that </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// trusts other domains.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 0x00000800;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// This is a computer account for a computer that is a member </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// of this domain.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_WORKSTATION_TRUST_ACCOUNT = 0x00001000;</div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// This is a computer account for a system backup domain controller </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// that is a member of this domain.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_SERVER_TRUST_ACCOUNT = 0x00002000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The password for this account will never expire.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_DONT_EXPIRE_PASSWD = 0x00010000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// This is an MNS logon account.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_MNS_LOGON_ACCOUNT = 0x00020000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The user must log on using a smart card.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_SMARTCARD_REQUIRED = 0x00040000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The service account (user or computer account), under which a </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// service runs, is trusted for Kerberos delegation. Any such service </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// can impersonate a client requesting the service.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_TRUSTED_FOR_DELEGATION = 0x00080000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The security context of the user will not be delegated to a service </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// even if the service account is set as trusted for Kerberos delegation.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_NOT_DELEGATED = 0x00100000;</div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// Restrict this principal to use only Data Encryption Standard (DES) </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// encryption types for keys.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_USE_DES_KEY_ONLY = 0x00200000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// This account does not require Kerberos pre-authentication for logon.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw4">int</span> ADS_UF_DONT_REQUIRE_PREAUTH = 0x00400000;</div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// The user password has expired. This flag is created by the system </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// using data from the Pwd-Last-Set attribute and the domain policy.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_PASSWORD_EXPIRED = 0x00800000;</div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// The account is enabled for delegation. This is a security-sensitive </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// setting; accounts with this option enabled should be strictly </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// controlled. This setting enables a service running under the account </span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// to assume a client identity and authenticate as that user to other </span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// remote servers on the network.</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li1"><div
class="de1"><span
class="kw4">int</span> ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 0x01000000;</div></li></ol></div></div></div><p></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=DCPBjTSyXoo:WnW5HIGNjNo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=DCPBjTSyXoo:WnW5HIGNjNo:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/DCPBjTSyXoo" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-7-handling-bit-property-useraccountcontrol-account-disable/450.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-7-handling-bit-property-useraccountcontrol-account-disable/450.html</feedburner:origLink></item> <item><title>Active Directory Tip #6 – Set Account never expires</title><link>http://feedproxy.google.com/~r/dun3/~3/o1MTUmteZGQ/446.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-6-set-account-never-expires/446.html#comments</comments> <pubDate>Mon, 10 May 2010 19:02:55 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[Active Directory]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=446</guid> <description><![CDATA[The account never expires checkbox in the MMC is a virtual option. Instead of being a separate property it is set using the accountExpires property. Setting it to the equivalent of 0 will result in an account that never expires. See Account-Expires Attribute for more details. PLAIN TEXT C#: /// &#60;summary&#62; /// You still have [...]]]></description> <content:encoded><![CDATA[<p>The account never expires checkbox in the MMC is a virtual option. Instead of being a separate property it is set using the accountExpires property. Setting it to the equivalent of 0 will result in an account that never expires.</p><p>See <a
href="http://msdn.microsoft.com/en-us/library/ms675098.aspx">Account-Expires Attribute</a> for more details.</p><div
class="igBar"><span
id="lcsharp-36"><a
href="#" onclick="javascript:showCodeTxt('csharp-36'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-36"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// You still have to call CommitChanges() after using this method.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw1">private</span> <span
class="kw1">void</span> SetAccountNeverExpires<span
class="br0">&#40;</span>DirectoryEntry directoryEntry<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw4">long</span> accountExpires = <span
class="nu0">0</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; var accountExpiresOriginalValue = directoryEntry.<span
class="me1">Properties</span><span
class="br0">&#91;</span><span
class="st0">"accountExpires"</span><span
class="br0">&#93;</span>.<span
class="me1">Value</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; Type type = accountExpiresOriginalValue.<span
class="me1">GetType</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw4">int</span> highPart = <span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span><span
class="br0">&#40;</span>accountExpires&gt;&gt; <span
class="nu0">32</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw4">int</span> lowPart = <span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span><span
class="br0">&#40;</span>0xFFFFFFFF &amp; accountExpires<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; type.<span
class="me1">InvokeMember</span><span
class="br0">&#40;</span><span
class="st0">"HighPart"</span>, BindingFlags.<span
class="me1">SetProperty</span>, <span
class="kw1">null</span>, accountExpiresOriginalValue, <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> <span
class="kw4">object</span><span
class="br0">&#91;</span><span
class="br0">&#93;</span> <span
class="br0">&#123;</span> highPart <span
class="br0">&#125;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; type.<span
class="me1">InvokeMember</span><span
class="br0">&#40;</span><span
class="st0">"LowPart"</span>, BindingFlags.<span
class="me1">SetProperty</span>, <span
class="kw1">null</span>, accountExpiresOriginalValue, <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> <span
class="kw4">object</span><span
class="br0">&#91;</span><span
class="br0">&#93;</span> <span
class="br0">&#123;</span> lowPart <span
class="br0">&#125;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; SetAdObjectAttribute<span
class="br0">&#40;</span>directoryEntry, <span
class="st0">"accountExpires"</span>, accountExpiresOriginalValue<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=o1MTUmteZGQ:AI-gp3Rp1EE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=o1MTUmteZGQ:AI-gp3Rp1EE:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/o1MTUmteZGQ" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-6-set-account-never-expires/446.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-6-set-account-never-expires/446.html</feedburner:origLink></item> <item><title>Active Directory Tip #5 – Set Large Integer (e.g. Account expires date)</title><link>http://feedproxy.google.com/~r/dun3/~3/z6A-emU_50M/444.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-5-set-large-integer-e-g-account-expires-date/444.html#comments</comments> <pubDate>Mon, 10 May 2010 12:00:11 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[Active Directory]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=444</guid> <description><![CDATA[Setting a large integer property is just as easy. PLAIN TEXT C#: /// &#60;summary&#62; /// You still have to call CommitChanges() after using this method. /// &#60;/summary&#62; private void SetAccountExpiresDate&#40;DirectoryEntry directoryEntry, DateTime expiresOn&#41; &#123; &#160; long accountExpires = expiresOn.ToFileTimeUtc&#40;&#41;; &#160; &#160; &#160; var accountExpiresOriginalValue = directoryEntry.Properties&#91;"accountExpires"&#93;.Value; &#160; &#160; Type type = accountExpiresOriginalValue.GetType&#40;&#41;; &#160; &#160; int [...]]]></description> <content:encoded><![CDATA[<p>Setting a large integer property is just as easy.</p><div
class="igBar"><span
id="lcsharp-38"><a
href="#" onclick="javascript:showCodeTxt('csharp-38'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-38"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="co1">/// You still have to call CommitChanges() after using this method.</span></div></li><li
class="li1"><div
class="de1"><span
class="co1">/// &lt;/summary&gt;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw1">private</span> <span
class="kw1">void</span> SetAccountExpiresDate<span
class="br0">&#40;</span>DirectoryEntry directoryEntry, DateTime expiresOn<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw4">long</span> accountExpires = expiresOn.<span
class="me1">ToFileTimeUtc</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; &nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; var accountExpiresOriginalValue = directoryEntry.<span
class="me1">Properties</span><span
class="br0">&#91;</span><span
class="st0">"accountExpires"</span><span
class="br0">&#93;</span>.<span
class="me1">Value</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; Type type = accountExpiresOriginalValue.<span
class="me1">GetType</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw4">int</span> highPart = <span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span><span
class="br0">&#40;</span>accountExpires&gt;&gt; <span
class="nu0">32</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw4">int</span> lowPart = <span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span><span
class="br0">&#40;</span>0xFFFFFFFF &amp; accountExpires<span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; type.<span
class="me1">InvokeMember</span><span
class="br0">&#40;</span><span
class="st0">"HighPart"</span>, BindingFlags.<span
class="me1">SetProperty</span>, <span
class="kw1">null</span>, accountExpiresOriginalValue, <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> <span
class="kw4">object</span><span
class="br0">&#91;</span><span
class="br0">&#93;</span> <span
class="br0">&#123;</span> highPart <span
class="br0">&#125;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; type.<span
class="me1">InvokeMember</span><span
class="br0">&#40;</span><span
class="st0">"LowPart"</span>, BindingFlags.<span
class="me1">SetProperty</span>, <span
class="kw1">null</span>, accountExpiresOriginalValue, <a
href="http://www.google.com/search?q=new+msdn.microsoft.com"><span
class="kw3">new</span></a> <span
class="kw4">object</span><span
class="br0">&#91;</span><span
class="br0">&#93;</span> <span
class="br0">&#123;</span> lowPart <span
class="br0">&#125;</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; SetAdObjectAttribute<span
class="br0">&#40;</span>directoryEntry, <span
class="st0">"accountExpires"</span>, accountExpiresOriginalValue<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=z6A-emU_50M:BupJQm7nqL4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=z6A-emU_50M:BupJQm7nqL4:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/z6A-emU_50M" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-5-set-large-integer-e-g-account-expires-date/444.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-5-set-large-integer-e-g-account-expires-date/444.html</feedburner:origLink></item> <item><title>Active Directory Tip #4 – Get Large Integer (e.g. Account expires date)</title><link>http://feedproxy.google.com/~r/dun3/~3/HdY_9RN3i1E/442.html</link> <comments>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-4-get-large-integer-e-g-account-expires-date/442.html#comments</comments> <pubDate>Tue, 04 May 2010 20:58:40 +0000</pubDate> <dc:creator>Tobias Hertkorn</dc:creator> <category><![CDATA[.NET]]></category> <category><![CDATA[Active Directory]]></category> <category><![CDATA[C#]]></category> <category><![CDATA[IT]]></category> <category><![CDATA[Programming]]></category><guid isPermaLink="false">http://saftsack.fs.uni-bayreuth.de/~dun3/?p=442</guid> <description><![CDATA[Certain properties, like usnChanged or accountExpires, are in an IADsLargeInteger format. One way to read out the long representation of the value of such a property is by reflection. PLAIN TEXT C#: private static long ConvertLargeIntegerToLong&#40;object largeInteger&#41; &#123; &#160; Type type = largeInteger.GetType&#40;&#41;; &#160; &#160; int highPart = &#40;int&#41;type.InvokeMember&#40;"HighPart", BindingFlags.GetProperty, null, largeInteger, null&#41;; &#160; int [...]]]></description> <content:encoded><![CDATA[<p>Certain properties, like usnChanged or accountExpires, are in an IADsLargeInteger format. One way to read out the long representation of the value of such a property is by reflection.</p><div
class="igBar"><span
id="lcsharp-41"><a
href="#" onclick="javascript:showCodeTxt('csharp-41'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-41"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw1">private</span> <span
class="kw1">static</span> <span
class="kw4">long</span> ConvertLargeIntegerToLong<span
class="br0">&#40;</span><span
class="kw4">object</span> largeInteger<span
class="br0">&#41;</span></div></li><li
class="li2"><div
class="de2"><span
class="br0">&#123;</span></div></li><li
class="li1"><div
class="de1">&nbsp; Type type = largeInteger.<span
class="me1">GetType</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp;</div></li><li
class="li1"><div
class="de1">&nbsp; <span
class="kw4">int</span> highPart = <span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span>type.<span
class="me1">InvokeMember</span><span
class="br0">&#40;</span><span
class="st0">"HighPart"</span>, BindingFlags.<span
class="me1">GetProperty</span>, <span
class="kw1">null</span>, largeInteger, <span
class="kw1">null</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw4">int</span> lowPart = <span
class="br0">&#40;</span><span
class="kw4">int</span><span
class="br0">&#41;</span>type.<span
class="me1">InvokeMember</span><span
class="br0">&#40;</span><span
class="st0">"LowPart"</span>, BindingFlags.<span
class="me1">GetProperty</span> | BindingFlags.<span
class="kw1">Public</span>, <span
class="kw1">null</span>, largeInteger, <span
class="kw1">null</span><span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">return</span> <span
class="br0">&#40;</span><span
class="kw4">long</span><span
class="br0">&#41;</span>highPart &lt;&lt;<span
class="nu0">32</span> | <span
class="br0">&#40;</span><span
class="kw4">uint</span><span
class="br0">&#41;</span>lowPart;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p><p>That value may be converted to a DateTime value using the static DateTime.FromFileTimeUtc method.</p><div
class="igBar"><span
id="lcsharp-42"><a
href="#" onclick="javascript:showCodeTxt('csharp-42'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">C#:</span><div
id="csharp-42"><div
class="csharp"><ol><li
class="li1"><div
class="de1"><span
class="kw4">object</span> accountExpires = DirectoryEntryHelper.<span
class="me1">GetAdObjectProperty</span><span
class="br0">&#40;</span>directoryEntry, <span
class="st0">"accountExpires"</span><span
class="br0">&#41;</span>;</div></li><li
class="li2"><div
class="de2">var asLong = ConvertLargeIntegerToLong<span
class="br0">&#40;</span>accountExpires<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1">&nbsp;</div></li><li
class="li2"><div
class="de2"><span
class="kw1">if</span> <span
class="br0">&#40;</span>asLong == <span
class="kw4">long</span>.<span
class="me1">MaxValue</span> || asLong &lt;= <span
class="nu0">0</span> || DateTime.<span
class="me1">MaxValue</span>.<span
class="me1">ToFileTime</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span> &lt;= asLong<span
class="br0">&#41;</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">return</span> DateTime.<span
class="me1">MaxValue</span>;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li><li
class="li2"><div
class="de2"><span
class="kw1">else</span></div></li><li
class="li1"><div
class="de1"><span
class="br0">&#123;</span></div></li><li
class="li2"><div
class="de2">&nbsp; <span
class="kw1">return</span> DateTime.<span
class="me1">FromFileTimeUtc</span><span
class="br0">&#40;</span>asLong<span
class="br0">&#41;</span>;</div></li><li
class="li1"><div
class="de1"><span
class="br0">&#125;</span></div></li></ol></div></div></div><p></p> <div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/dun3?a=HdY_9RN3i1E:E6vu34GH8vE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/dun3?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/dun3?a=HdY_9RN3i1E:E6vu34GH8vE:lwXqHt5wHsE"><img src="http://feeds.feedburner.com/~ff/dun3?d=lwXqHt5wHsE" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/dun3/~4/HdY_9RN3i1E" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-4-get-large-integer-e-g-account-expires-date/442.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> <feedburner:origLink>http://www.fsmpi.uni-bayreuth.de/~dun3/archives/active-directory-tip-4-get-large-integer-e-g-account-expires-date/442.html</feedburner:origLink></item> </channel> </rss><!-- This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk
Database Caching 4/10 queries in 0.005 seconds using disk

Served from: btfmx2.fs.uni-bayreuth.de @ 2012-02-03 00:39:39 -->

