<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8776240752751469943</id><updated>2024-09-13T20:39:07.143+03:00</updated><category term="Extension methods"/><category term="c# extension methods"/><category term="c# extensions"/><category term="c# stream"/><category term="validate image"/><category term="validate picture"/><category term="DataGridView"/><category term="IDictionary"/><category term="Reflection"/><category term="file"/><category term="validate video"/><title type='text'>Out of memory</title><subtitle type='html'>This is no blog. This is a place for some useful code that I may forget in near future.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://mynkow.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://mynkow.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>mynkow</name><uri>http://www.blogger.com/profile/17286274614098994349</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8776240752751469943.post-7151065489064250497</id><published>2010-05-04T16:55:00.006+03:00</published><updated>2012-04-08T22:31:57.664+03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c# stream"/><category scheme="http://www.blogger.com/atom/ns#" term="file"/><category scheme="http://www.blogger.com/atom/ns#" term="validate image"/><category scheme="http://www.blogger.com/atom/ns#" term="validate picture"/><category scheme="http://www.blogger.com/atom/ns#" term="validate video"/><title type='text'>How to validate video file</title><content type='html'>&lt;div id=&quot;content&quot;&gt;&lt;p&gt;In my previous post I showed you &lt;a href=&quot;http://mynkow.blogspot.com/2010/04/validate-image.html&quot;&gt;how to validate an image/picture file&lt;/a&gt;. Today I will add a video support to this validation and I will refactor the class a bit. Just know that video validation use exactly the same logic.&lt;/p&gt;&lt;pre name=&quot;code&quot; class=&quot;c#&quot;&gt;    public enum FileType&lt;br /&gt;    {&lt;br /&gt;        Picture,&lt;br /&gt;        Audio,&lt;br /&gt;        Video,&lt;br /&gt;        Text,&lt;br /&gt;        Other,&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static class FileValidator&lt;br /&gt;    {&lt;br /&gt;        private static Dictionary&amp;lt;FileType, byte[][]&amp;gt; _definedHeaders = new Dictionary&amp;lt;FileType, byte[][]&amp;gt;( 2 );&lt;br /&gt;&lt;br /&gt;        //  Defined video headers.&lt;br /&gt;        private static byte[][] _videoHeaders = new byte[][]&lt;br /&gt;        {&lt;br /&gt;            new byte[]{ 0x41, 0x56, 0x49, 0x20 }, // .avi&lt;br /&gt;            new byte[]{ 0x52, 0x49, 0x46, 0x46 }, // .avi&lt;br /&gt;            new byte[]{ 0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62 }, // .asf, .wmv&lt;br /&gt;            new byte[]{ 0x00, 0x00, 0x01, 0xB3 }, // .mpg&lt;br /&gt;            new byte[]{ 0x00, 0x00, 0x01, 0xba}, // .mpg&lt;br /&gt;            new byte[]{ 0x00, 0x00, 0x00, 0x18}, // .mp4&lt;br /&gt;            new byte[]{ 0x46, 0x4C, 0x56 } // .flv&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;        //  Defined image headers.&lt;br /&gt;        private static byte[][] _imageHeaders = new byte[][]&lt;br /&gt;        {&lt;br /&gt;            new byte[]{ 0xFF, 0xD8 },                                       //  .jpg, .jpeg, .jpe, .jfif, .jif&lt;br /&gt;            new byte[]{ 0x42, 0x4D},                                        //  .bmp&lt;br /&gt;            new byte[]{ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A },   //  .png&lt;br /&gt;            new byte[]{ 0x47, 0x49, 0x46 }                                  //  .gif&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;        static FileValidator()&lt;br /&gt;        {&lt;br /&gt;            RegisterHeaders();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private static void RegisterHeaders()&lt;br /&gt;        {&lt;br /&gt;            _definedHeaders.Add( FileType.Picture, _imageHeaders );&lt;br /&gt;            _definedHeaders.Add( FileType.Video, _videoHeaders );&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        /// &amp;lt;summary&amp;gt;&lt;br /&gt;        /// Validate that the stream is of an expected type.&lt;br /&gt;        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;        /// &amp;lt;remarks&amp;gt;&lt;br /&gt;        /// IMPORTANT: The calling code is responsible for creating and disposing the stream.&lt;br /&gt;        /// &amp;lt;/remarks&amp;gt;&lt;br /&gt;        /// &amp;lt;param name=&amp;quot;theStream&amp;quot;&amp;gt;The stream of a file.&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;exception cref=&amp;quot;Exception&amp;quot;&amp;gt;Throws if the stream is of invalid file.&amp;lt;/exception&amp;gt;&lt;br /&gt;        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;br /&gt;        public static bool IsValidFile( Stream theStream, FileType ofTFileype )&lt;br /&gt;        {&lt;br /&gt;            if( theStream.Length &amp;gt; 0 )&lt;br /&gt;            {&lt;br /&gt;                byte[] header = new byte[ 8 ]; // Change size if needed.&lt;br /&gt;                theStream.Read( header, 0, header.Length );&lt;br /&gt;&lt;br /&gt;                bool hasHeader = _definedHeaders[ ofTFileype ].Count( magic =&amp;gt;&lt;br /&gt;                {&lt;br /&gt;                    int i = 0;&lt;br /&gt;                    if( magic.Length &amp;gt; header.Length )&lt;br /&gt;                        return false;&lt;br /&gt;                    return magic.Count( b =&amp;gt; { return b == header[ i++ ]; } ) == magic.Length;&lt;br /&gt;                } ) &amp;gt; 0;&lt;br /&gt;&lt;br /&gt;                return hasHeader;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            return false;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public static bool IsValidFile( FileInfo fileInfo, FileType ofFileType )&lt;br /&gt;        {&lt;br /&gt;            bool result = fileInfo.Exists;&lt;br /&gt;&lt;br /&gt;            if( IsFileTypeRegistered( ofFileType ) )&lt;br /&gt;            {&lt;br /&gt;                using( Stream stream = fileInfo.OpenRead() )&lt;br /&gt;                {&lt;br /&gt;                    result = result ? FileValidator.IsValidFile( stream, ofFileType ) : false;&lt;br /&gt;&lt;br /&gt;                    if( !result )&lt;br /&gt;                        throw new Exception( string.Format( &amp;quot;Invalid {0} file. {1}&amp;quot;, ofFileType.NameToString(), fileInfo.FullName ) );&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            return result;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private static bool IsFileTypeRegistered( FileType fileType )&lt;br /&gt;        {&lt;br /&gt;            return _definedHeaders.ContainsKey( fileType );&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public static bool IsValidFile( string file, FileType ofFileType )&lt;br /&gt;        {&lt;br /&gt;            return IsValidFile( new FileInfo( file ), ofFileType );&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mynkow.blogspot.com/feeds/7151065489064250497/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mynkow.blogspot.com/2010/05/how-to-validate-video-file.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default/7151065489064250497'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default/7151065489064250497'/><link rel='alternate' type='text/html' href='http://mynkow.blogspot.com/2010/05/how-to-validate-video-file.html' title='How to validate video file'/><author><name>mynkow</name><uri>http://www.blogger.com/profile/17286274614098994349</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8776240752751469943.post-114855020074676635</id><published>2010-04-29T17:03:00.003+03:00</published><updated>2010-04-29T17:23:55.232+03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c# stream"/><category scheme="http://www.blogger.com/atom/ns#" term="validate image"/><category scheme="http://www.blogger.com/atom/ns#" term="validate picture"/><title type='text'>Validate image</title><content type='html'>&lt;div id=&quot;content&quot;&gt;&lt;p&gt;When we work with files we use stream. The problem is that you expect a stream of an image/picture but actually you get something else - probably a stream of a renamed 500GB iso file. So it is a good practice to validate and be sure that the file is actually an image/picture before reading the whole file. This is a simple task because each image/picture type has a header with unique value at the beginning if the file. So we will get the first few bytes from our file and compare them with statically defined headers for each image/picture type. These statically defined headers can be googled easily.&lt;/p&gt;&lt;pre name=&quot;code&quot; class=&quot;c#&quot;&gt;        //  Defined image headers.&lt;br /&gt;        private static byte[][] _imageHeaders = new byte[][]&lt;br /&gt;        {&lt;br /&gt;            new byte[]{ 0xFF, 0xD8 },                                       //  .jpg, .jpeg, .jpe, .jfif, .jif&lt;br /&gt;            new byte[]{ 0x42, 0x4D},                                        //  .bmp&lt;br /&gt;            new byte[]{ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A },   //  .png&lt;br /&gt;            new byte[]{ 0x47, 0x49, 0x46 }                                  //  .gif&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;        /// &amp;lt;summary&amp;gt;&lt;br /&gt;        /// Validate that the stream is of an image file.&lt;br /&gt;        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;        /// &amp;lt;remarks&amp;gt;&lt;br /&gt;        /// IMPORTANT: The calling code is responsible for creating and disposing the image stream.&lt;br /&gt;        /// Supported file types: .JPEG .BMP .GIF .PNG&lt;br /&gt;        /// &amp;lt;/remarks&amp;gt;&lt;br /&gt;        /// &amp;lt;param name=&amp;quot;imageStream&amp;quot;&amp;gt;The stream of a picture file.&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;exception cref=&amp;quot;Exception&amp;quot;&amp;gt;Throws if the stream is of invalid image.&amp;lt;/exception&amp;gt;&lt;br /&gt;        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;br /&gt;        public static bool IsValidImage( Stream imageStream )&lt;br /&gt;        {&lt;br /&gt;            if( imageStream.Length &amp;gt; 0 )&lt;br /&gt;            {&lt;br /&gt;                byte[] header = new byte[ 8 ]; // Change size if needed.&lt;br /&gt;                imageStream.Read( header, 0, header.Length );&lt;br /&gt;&lt;br /&gt;                bool hasImageHeader = _imageHeaders.Count( magic =&amp;gt;&lt;br /&gt;                {&lt;br /&gt;                    int i = 0;&lt;br /&gt;                    if( magic.Length &amp;gt; header.Length )&lt;br /&gt;                        return false;&lt;br /&gt;                    return magic.Count( b =&amp;gt; { return b == header[ i++ ]; } ) == magic.Length;&lt;br /&gt;                } ) &amp;gt; 0;&lt;br /&gt;&lt;br /&gt;                return hasImageHeader;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            return false;&lt;br /&gt;        }&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mynkow.blogspot.com/feeds/114855020074676635/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mynkow.blogspot.com/2010/04/validate-image.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default/114855020074676635'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default/114855020074676635'/><link rel='alternate' type='text/html' href='http://mynkow.blogspot.com/2010/04/validate-image.html' title='Validate image'/><author><name>mynkow</name><uri>http://www.blogger.com/profile/17286274614098994349</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8776240752751469943.post-4214757392036440182</id><published>2009-11-22T22:44:00.018+02:00</published><updated>2017-07-12T10:23:27.390+03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c# extension methods"/><category scheme="http://www.blogger.com/atom/ns#" term="c# extensions"/><category scheme="http://www.blogger.com/atom/ns#" term="Extension methods"/><category scheme="http://www.blogger.com/atom/ns#" term="Reflection"/><title type='text'>.NET C# Extension Methods - Reflection</title><content type='html'>&lt;div id=&quot;content&quot;&gt;
&lt;strong&gt;Other extension methods:&lt;/strong&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2009/11/net-c-extension-methods.html&quot;&gt;DataGridView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2009/11/net-c-extension-methods-idictionary.html&quot;&gt;Dictionary&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2010/05/net-c-extension-methods-enum.html&quot;&gt;Enum&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2009/11/net-c-extension-methods-reflection.html&quot;&gt;Reflection&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
---------------------------&lt;br /&gt;
I was wondering how to get the name of a property via reflection. So I searched the Internet and I found &lt;a href=&quot;http://code.google.com/p/gim-projects/source/browse/presentations/CantDanceTheLambda/src/MemberNameParser.cs&quot; target=&quot;_blank&quot;&gt;this&lt;/a&gt; on &lt;a href=&quot;http://code.google.com/&quot; target=&quot;_blank&quot;&gt;Google code&lt;/a&gt;. The code was not good enough though. I was not able to get a string name from level 3 (i.e: User.Name.First). I did a small modifications and ended with this class that works perfect.&lt;br /&gt;
&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;    public static class MembersOf&lt;t&gt;
    {
        public static string GetPath&lt;r&gt;(System.Linq.Expressions.Expression&lt;system .func=&quot;&quot; r=&quot;&quot;&gt;&amp;gt; expr)
        {
            var node = expr.Body as System.Linq.Expressions.MemberExpression;
            if (ReferenceEquals(null, node))
                throw new System.InvalidOperationException(&quot;Expression must be of member access&quot;);

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string result = BuildName(node, sb).Remove(0, 1).ToString();
            sb.Clear();

            return result;
        }

        static System.Text.StringBuilder BuildName(System.Linq.Expressions.MemberExpression expr, System.Text.StringBuilder sb)
        {
            if (ReferenceEquals(null, expr) == false)
            {
                sb.Insert(0, expr.Member.Name);
                sb.Insert(0, &quot;.&quot;);

                BuildName(expr.Expression as System.Linq.Expressions.MemberExpression, sb);
            }

            return sb;
        }
    }&lt;/system&gt;&lt;/r&gt;&lt;/t&gt;&lt;/pre&gt;
Now the question is where this code can be handy? I use this when I want to build NHibernate Criteria or DetachedCriteria. When you create a Criteria you use a code like this one:&lt;br /&gt;
&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;
ICriteria crit = session.CreateCriteria(typeof(Customer));
crit.Add(Expression.Eq(&quot;Customer.Name.First&quot;, someValue));&lt;/pre&gt;
Here the Name property is a NHibernate component. The problem is that if you change the property Name to FirstName you will not get a compile error but you will get runtime error when the query is executed against the database. Also you can use some refactoring tool like RefactorPro! and the changes in the component will be applied also in the criteria. Nice, ahh?!? And the code above will be:&lt;br /&gt;
&lt;pre class=&quot;c#&quot; name=&quot;code&quot;&gt;
ICriteria crit = session.CreateCriteria(typeof(Customer));
crit.Add(Expression.Eq(MembersOf&amp;lt;Customer&amp;gt;.GetPath(x=&amp;gt;x.Name.First), someValue));&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://mynkow.blogspot.com/feeds/4214757392036440182/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mynkow.blogspot.com/2009/11/net-c-extension-methods-reflection.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default/4214757392036440182'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default/4214757392036440182'/><link rel='alternate' type='text/html' href='http://mynkow.blogspot.com/2009/11/net-c-extension-methods-reflection.html' title='.NET C# Extension Methods - Reflection'/><author><name>mynkow</name><uri>http://www.blogger.com/profile/17286274614098994349</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8776240752751469943.post-8633752660728223626</id><published>2009-11-19T11:14:00.008+02:00</published><updated>2010-05-12T14:20:37.787+03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c# extension methods"/><category scheme="http://www.blogger.com/atom/ns#" term="c# extensions"/><category scheme="http://www.blogger.com/atom/ns#" term="Extension methods"/><category scheme="http://www.blogger.com/atom/ns#" term="IDictionary"/><title type='text'>.NET C# Extension Methods - IDictionary</title><content type='html'>&lt;div id=&quot;content&quot;&gt;&lt;p&gt;&lt;strong&gt;Other extension methods:&lt;/strong&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2009/11/net-c-extension-methods.html&quot;&gt;DataGridView&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2009/11/net-c-extension-methods-idictionary.html&quot;&gt;Dictionary&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2010/05/net-c-extension-methods-enum.html&quot;&gt;Enum&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2009/11/net-c-extension-methods-reflection.html&quot;&gt;Reflection&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;p&gt;---------------------------&lt;/p&gt;&lt;p&gt;Extension method for IDictionary. Returns the values associated with the key specified by parameter &lt;span style=&quot;font-style: italic;&quot;&gt;key&lt;/span&gt;.&lt;/p&gt;&lt;pre name=&quot;code&quot; class=&quot;c#&quot;&gt;        /// &amp;lt;summary&amp;gt;&lt;br /&gt;        /// Gets the value of an element within a dictionary object.&lt;br /&gt;        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;        /// &amp;lt;typeparam name=&amp;quot;TValue&amp;quot;&amp;gt;The type of the value.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;        /// &amp;lt;param name=&amp;quot;bag&amp;quot;&amp;gt;The actual dictionary.&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;param name=&amp;quot;key&amp;quot;&amp;gt;The key that will be searched for within the dictionary object.&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;param name=&amp;quot;defaultValue&amp;quot;&amp;gt;Returns default value if the specified key was not found.&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;returns&amp;gt;Specific value within dictionary object.&amp;lt;/returns&amp;gt;&lt;br /&gt;        public static TValue GetValue&amp;lt;TValue&amp;gt;( this IDictionary bag, string key, TValue defaultValue )&lt;br /&gt;        {&lt;br /&gt;            object value = bag[ key ];&lt;br /&gt;&lt;br /&gt;            if( value == null )&lt;br /&gt;            {&lt;br /&gt;                value = defaultValue;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            return defaultValue;&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Extension method for IDictionary. Adds a key-value pair.&lt;/p&gt;&lt;pre name=&quot;code&quot; class=&quot;c#&quot;&gt;        /// &amp;lt;summary&amp;gt;&lt;br /&gt;        /// Adds a (key,value) pair in a dictionary object.&lt;br /&gt;        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;        /// &amp;lt;typeparam name=&amp;quot;TKey&amp;quot;&amp;gt;The type of keys in the dictionary.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;        /// &amp;lt;typeparam name=&amp;quot;TValue&amp;quot;&amp;gt;The type of values in the dictionary.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;        /// &amp;lt;param name=&amp;quot;bag&amp;quot;&amp;gt;The actual dictionary.&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;param name=&amp;quot;key&amp;quot;&amp;gt;The name of the key used for easy navigation in a dictionary object.&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;param name=&amp;quot;value&amp;quot;&amp;gt;The value assoiciated with the key.&amp;lt;/param&amp;gt;&lt;br /&gt;        public static void SetValue&amp;lt;TKey, TValue&amp;gt;( this IDictionary&amp;lt;TKey,TValue&amp;gt; bag, TKey key, TValue value )&lt;br /&gt;        {&lt;br /&gt;            if( bag.ContainsKey( key ) )&lt;br /&gt;            {&lt;br /&gt;                bag[ key ] = value;&lt;br /&gt;            }&lt;br /&gt;            else&lt;br /&gt;            {&lt;br /&gt;                bag.Add( key, value );&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mynkow.blogspot.com/feeds/8633752660728223626/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mynkow.blogspot.com/2009/11/net-c-extension-methods-idictionary.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default/8633752660728223626'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default/8633752660728223626'/><link rel='alternate' type='text/html' href='http://mynkow.blogspot.com/2009/11/net-c-extension-methods-idictionary.html' title='.NET C# Extension Methods - IDictionary'/><author><name>mynkow</name><uri>http://www.blogger.com/profile/17286274614098994349</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8776240752751469943.post-6247338169578932720</id><published>2009-11-19T09:35:00.018+02:00</published><updated>2010-05-12T14:21:03.316+03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c# extension methods"/><category scheme="http://www.blogger.com/atom/ns#" term="c# extensions"/><category scheme="http://www.blogger.com/atom/ns#" term="DataGridView"/><category scheme="http://www.blogger.com/atom/ns#" term="Extension methods"/><title type='text'>.NET C# Extension Methods - DataGridView</title><content type='html'>&lt;div id=&quot;content&quot;&gt;&lt;p&gt;&lt;strong&gt;Other extension methods:&lt;/strong&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2009/11/net-c-extension-methods.html&quot;&gt;DataGridView&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2009/11/net-c-extension-methods-idictionary.html&quot;&gt;Dictionary&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2010/05/net-c-extension-methods-enum.html&quot;&gt;Enum&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://mynkow.blogspot.com/2009/11/net-c-extension-methods-reflection.html&quot;&gt;Reflection&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;p&gt;---------------------------&lt;/p&gt;&lt;p&gt;Extension method for DataGridView. Returns IDictionary with values of all selected rows and columns that are specified by parameter &lt;span style=&quot;font-style: italic;&quot;&gt;columnName&lt;/span&gt;.&lt;/p&gt;&lt;pre name=&quot;code&quot; class=&quot;c#&quot;&gt;        /// &amp;lt;summary&amp;gt;&lt;br /&gt;        /// Gets values from selected rows and specified columns.&lt;br /&gt;        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;        /// &amp;lt;param name=&amp;quot;grid&amp;quot;&amp;gt;The grid.&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;param name=&amp;quot;columnName&amp;quot;&amp;gt;Name of the column/s.&amp;lt;/param&amp;gt;&lt;br /&gt;        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;br /&gt;        public static IList&amp;lt;IDictionary&amp;lt;string, object&amp;gt;&amp;gt; GetSelectedValuesFrom( this DataGridView grid, params string[] columnName )&lt;br /&gt;        {&lt;br /&gt;            IList&amp;lt;IDictionary&amp;lt;string,object&amp;gt;&amp;gt; results = new List&amp;lt;IDictionary&amp;lt;string, object&amp;gt;&amp;gt;( grid.SelectedRows.Count );&lt;br /&gt;&lt;br /&gt;            foreach( DataGridViewRow row in grid.SelectedRows )&lt;br /&gt;            {&lt;br /&gt;                IDictionary&amp;lt;string,object&amp;gt; rowResult = new Dictionary&amp;lt;string, object&amp;gt;( columnName.Length );&lt;br /&gt;&lt;br /&gt;                foreach( string column in columnName )&lt;br /&gt;                {&lt;br /&gt;                    rowResult.Add( column, row.Cells[ column ] );&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                results.Add( rowResult );&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            return results;&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Extension method for DataGridView. Returns a list with values of all selected rows in the columns specified by parameter &lt;span style=&quot;font-style: italic;&quot;&gt;columnName&lt;/span&gt;.&lt;/p&gt;&lt;pre name=&quot;code&quot; class=&quot;c#&quot;&gt;        public static IList&amp;lt;T&amp;gt; GetSelectedValuesFrom&amp;lt;T&amp;gt;( this DataGridView grid, string columnName )&lt;br /&gt;       {&lt;br /&gt;           IList&amp;lt;T&amp;gt; results = new List&amp;lt;T&amp;gt;( grid.SelectedRows.Count );&lt;br /&gt;&lt;br /&gt;           foreach( DataGridViewRow row in grid.SelectedRows )&lt;br /&gt;               results.Add( GetCurrentValueFrom&amp;lt;T&amp;gt;( grid, columnName ) );&lt;br /&gt;&lt;br /&gt;           return results;&lt;br /&gt;       }&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Extension method for DataGridView. Returns a single value of selected row in the columns specified by parameter &lt;span style=&quot;font-style: italic;&quot;&gt;columnName&lt;/span&gt;.&lt;/p&gt;&lt;pre name=&quot;code&quot; class=&quot;c#&quot;&gt;        public static T GetCurrentValueFrom&amp;lt;T&amp;gt;( this DataGridView grid, string columnName )&lt;br /&gt;        {&lt;br /&gt;            if (grid.CurrentRow != null)&lt;br /&gt;            {&lt;br /&gt;                return (T)Convert.ChangeType(grid.CurrentRow.Cells[columnName].Value, typeof(T));&lt;br /&gt;            }&lt;br /&gt;            return (T) Convert.ChangeType(- 1,typeof(T));&lt;br /&gt;        }&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mynkow.blogspot.com/feeds/6247338169578932720/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mynkow.blogspot.com/2009/11/net-c-extension-methods.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default/6247338169578932720'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8776240752751469943/posts/default/6247338169578932720'/><link rel='alternate' type='text/html' href='http://mynkow.blogspot.com/2009/11/net-c-extension-methods.html' title='.NET C# Extension Methods - DataGridView'/><author><name>mynkow</name><uri>http://www.blogger.com/profile/17286274614098994349</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>