Showing posts with label Reflection. Show all posts
Showing posts with label Reflection. Show all posts

Tuesday, 10 April 2012

Useful tips

1. How to load all the assemblies?

var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();

2. Combine absolute & relative paths

var relativePath = "..\\bling.txt";
var baseDirectory = "C:\\blah\\";
var absolutePath = Path.GetFullPath(baseDirectory + relativePath);

3. Get x86 Program folder

    static string ProgramFilesx86()
    {
       if( 8 == IntPtr.Size
       || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable( 
                "PROCESSOR_ARCHITEW6432"))))
       {
            return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
       }

        return Environment.GetEnvironmentVariable("ProgramFiles");
    }

4. Delete a windows server

   sc delete [servicename]

5. Sometimes Server.Databases[name].Drop() will fail with 'Currently in use' error 
   or FailedOperationException. These scenarios we have to 'Server.KillDatabase()).

6. Script to create User with server admin role

 USE [master];
 GO
 CREATE LOGIN SimpleUser
    WITH PASSWORD    = N'SimpleUser',
    CHECK_POLICY     = OFF,
    CHECK_EXPIRATION = OFF;
 GO
 EXEC sp_addsrvrolemember
    @loginame = N'SimpleUser',
    @rolename = N'sysadmin';

7. Sn -T [assembly] to get PublicKeyToken.

8. Remove database(s) based on name

DECLARE @Temp TABLE (Id INT IDENTITY(1, 1), name nvarchar(max));
DECLARE @RowCounter INT;
DECLARE @i INT;

INSERT INTO @Temp select name from sys.databases where name like N'GS_81%'

SELECT @RowCounter = MAX(Id) FROM @Temp

SET @i = 1

DECLARE @DBDeleteByName NVARCHAR(max);

WHILE @i <= @RowCounter
BEGIN
    SELECT @DBDeleteByName = 'DROP DATABASE [' + name + ']'  FROM @Temp WHERE Id = @i

    EXEC sp_executesql @DBDeleteByName;

    SET @i = @i + 1;
END

Friday, 21 October 2011

Static Reflection

Here is my example on static reflection(i have used INotifyPropertyChanged, but a simple code as i don't pass around the interface)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.ComponentModel;

namespace StaticReflection
{
public class Customer : INotifyPropertyChanged
{
private string employer = String.Empty;
private string mobile = String.Empty;

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}

public string Employer
{
get { return this.employer; }
set
{
if (value != this.employer)
{
this.employer = value;
NotifyPropertyChanged(this.PropertyName(c => c.Employer));
}
}
}

public string Mobile
{
get { return this.mobile; }

set
{
if (value != this.mobile)
{
this.mobile = value;
NotifyPropertyChanged(this.PropertyName(c => c.Mobile));
}
}
}
}

public static class CustomerExtensions
{
public static string PropertyName(this Customer target, Expression<Func<Customer, object>> expression)
{
return new PropertySpecifier<Customer>(expression).PropertyName;
}
}

public class PropertySpecifier<T>
{
//http://joelabrahamsson.com/entry/getting-property-and-method-names-using-static-reflection-in-c-sharp - Use this article to build a accomplished! PropertySpecifier function
public PropertySpecifier(Expression<Func<T, object>> expression)
{
if (expression == null) return;

if (expression.Body is MemberExpression)
{
this.PropertyName = (expression.Body as MemberExpression).Member.Name;
}
}

public string PropertyName
{
get;
set;
}
}
}