Using Extension Methods And A Custom Attribute To Extend System.Windows.Form.Control

net_logo

June 18, 2015 | Posted in .Net

I needed a way to mark any control in the system so that I could check if I needed to perform some action on it. Ideally it needed to be on the base class System.Windows.Forms.Control so that this could be done on anything that inherits from it.

I used two separate extension methods to add and check for the custom attribute using the TypeDescriptor class, and then:

To add the attribute to the control with the ignoreAction value:

YourControl.IgnoreAction(True)

To check for the attribute on the control elsewhere in the system:

if (Not YourControl.IgnoreAction) or (!YourControl.IgnoreAction) if using c#

The extension methods:

Public Module Extensions
Private ReadOnly attr As New IgnoreActionAttribute(True)

<System.Runtime.CompilerServices.Extension()>
Public Sub IgnoreAction(ByVal value As System.Windows.Forms.Control, ignore As Boolean)

Try
Dim params() As Attribute = {New IgnoreActionAttribute(ignore)}

System.ComponentModel.TypeDescriptor.AddAttributes(value, params)

Catch ex As Exception
‘Handle Error
End Try

End Sub

<System.Runtime.CompilerServices.Extension()>
Public Function IgnoreAction(ByVal value As System.Windows.Forms.Control) As Boolean

Dim ignore As Boolean = False

Dim attribs As System.ComponentModel.AttributeCollection = Nothing

Try
attribs = System.ComponentModel.TypeDescriptor.GetAttributes(value)

If attribs.Contains(attr) Then
ignore = True
End If

Catch ex As Exception
‘Handle Error
End Try

Return ignore

End Function

End Module

The custom attribute:

Public Class IgnoreActionAttribute
Inherits Attribute

”’ <summary>
”’ The ignore action value.
”’ </summary>
”’ <remarks></remarks>
Private _ignoreAction As Boolean

”’ <summary>
”’ Main constructor for the ignore action attribute.
”’ </summary>
”’ <param name=”ignoreAction”></param>
”’ <remarks></remarks>
Public Sub New(ignoreAction As Boolean)
Me._ignoreAction = ignoreAction
End Sub

”’ <summary>
”’ The ignore action value.
”’ </summary>
”’ <returns>True if the action should be ignored.</returns>
”’ <remarks></remarks>
Public Property IgnoreAction() As Boolean
Get
Return Me._ignoreAction
End Get
Set(value As Boolean)
Me._ignoreAction = value
End Set
End Property

End Class

I’ve seen other implementations which are far more convoluted than this. Here you can quickly and easily add your attribute to a control, or you could extend it to add any custom attribute to any object you like and then use the TypeDescriptor to check for it.

Read More →

How To Script Data From A SQL Server Table

sql server 2014

June 17, 2015 | Posted in SQL Server

I wanted a way to script off the data in a sql table in some automated fashion rather than having to do it manually and it turns out there is a way to do this in SSMS:

- Right-click on the DB, select Generate Scripts
- Click <Next> through the Introduction screen
- Select Specific database objects then your table, then <Next>
- Click <Advanced> then for “Types of Data to script” select “Schema and Data” then <OK>
- Then save to file\clipboard or new window

It will script off even things like identities so you’ll need to change that if it’s not something you require.

Read More →

Visual Studio 2012 Post Build Event – Run Batch – Validate Entity Framework Model ProviderManifestToken

Visual Studio 2013

June 3, 2015 | Posted in Visual Studio 2012, Windows

When entity framework models are refreshed from a SQL database, the ProviderManifestToken will change based on the version of SQL Server.

It was necessary to automate a process whereby every time the DAL built, I needed to verify that a refresh hadn’t inadvertently changed the ProviderManifestToken to an incorrect version.

To do this, I created a batch file (all edmx files must conform to SQL Server version 2005, and if they don’t, notify):

 

 

@echo off
@break off
@title Create folder with batch but only if it doesn't already exist - PROVIDER MANIFEST SEARCH
@color 0a
@cls

setlocal EnableDelayedExpansion

set shouldpause=0

if exist "C:\ProviderManifestTokens\" (
  rmdir "C:\ProviderManifestTokens\" /s /q

)

if not exist "C:\ProviderManifestTokens\" (
  mkdir "C:\ProviderManifestTokens\"  
)

set SOURCE_DIR=%cd%
set DEST_DIR=C:\ProviderManifestTokens
set FILENAMES_TO_COPY=*.edmx

for /R "%SOURCE_DIR%" %%F IN (%FILENAMES_TO_COPY%) do (
    if exist "%%F" (
    	set FILE_DIR=%%~dpF
    	set FILE_INTERMEDIATE_DIR=!FILE_DIR:%SOURCE_DIR%=!
    	xcopy /I /Y "%%F" "%DEST_DIR%"
    )
)

if exist "C:\ProviderManifestTokens\" (

  chdir /d C:\ProviderManifestTokens\

  ren *.edmx *.txt

  for %%F in (.\*) do (
  find "ProviderManifestToken=""2005" %%F > nul
        if errorlevel 1 (		

		echo ----------------------------------------------------------
		echo Warning: File %%F contains incorrect ProviderManifestToken
		echo ----------------------------------------------------------
		set shouldpause=1
    )
  )
)

if not %shouldpause% == 1 (
	echo ----------------------------------------------------------
	echo Success: *.edmx files valid 
	echo ----------------------------------------------------------
)

exit

 

 

 

 

The script essentially scans directories and sub directories for all edmx files, it then copies them to a folder on the root, changes file extensions from *.edmx to *.txt (so that I could perform string lookups) and then notify where the ProviderManifestToken wasn’t what I wanted it to be.

The only other bit is to add the following in to your project Build Events:

cd $(ProjectDir)
call CheckProviderManifestTokens.bat

The above is a Post Build event and is set to run Always.

Check your Output window for the results of the batch file.

Update: I ended up leaving the batch in the DAL where it was, and putting the post build event in to the main application project, as that project is the last to compile, therefore developers will see the results at the end in the output window.

The post build event changed everso slightly:

cd $(ProjectDir)\DalFolder
call CheckProviderManifestTokens.bat

Read More →