Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Sources/ClassMemberInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Reflection;

namespace MethodRedirect
{
public class ClassMemberInfo
{
MethodInfo[] _methods;
public MethodInfo[] Methods
{
get { return _methods; }
}

private ClassMemberInfo(Type type, string name, bool isMethod, Type[] types = null, bool isStatic = false)
{
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public;
if (isStatic)
{
flags |= BindingFlags.Static;
}
else
{
flags |= BindingFlags.Instance;
}

if (isMethod)
{
if (types != null)
{
_methods = new MethodInfo[] { type.GetMethod(name, types) };
}
else
{
_methods = new MethodInfo[] { type.GetMethod(name, flags) };
}
}
else
{
var propInfo = type.GetProperty(name, flags);
_methods = new MethodInfo[] { propInfo.GetGetMethod(true), propInfo.GetSetMethod(true) };
}
}

private ClassMemberInfo(params MethodInfo[] methods )
{
_methods = methods;
}

static public ClassMemberInfo FromMethodInfo(MethodInfo mi)
{
return new ClassMemberInfo(mi);
}

static public ClassMemberInfo FromFunc<T>(Func<T> target)
{
return new ClassMemberInfo(target.Method);
}
static public ClassMemberInfo FromFunc<T, R>(Func<T, R> target)
{
return new ClassMemberInfo(target.Method);
}

static public ClassMemberInfo FromMethod(Type type, string methodName, Type[] types)
{
return new ClassMemberInfo(type, methodName, true, types);
}

static public ClassMemberInfo FromMethod(Type type, string methodName, bool isStatic = false)
{
return new ClassMemberInfo(type, methodName, true, null, isStatic);
}

static public ClassMemberInfo FromProperty(Type type, string methodName, bool isStatic = false)
{
return new ClassMemberInfo(type, methodName, false, null, isStatic);
}
}
}
40 changes: 0 additions & 40 deletions Sources/MethodOperation.cs

This file was deleted.

200 changes: 98 additions & 102 deletions Sources/MethodRedirect.csproj
Original file line number Diff line number Diff line change
@@ -1,103 +1,99 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2A84DC12-471E-492F-B390-DF3ADB452E28}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>MethodRedirect</RootNamespace>
<AssemblyName>MethodRedirect</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<StartupObject>MethodRedirect.Scenario1</StartupObject>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Optimize>false</Optimize>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>false</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<UseVSHostingProcess>true</UseVSHostingProcess>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MethodOperation.cs" />
<Compile Include="MethodToken.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scenario6.cs" />
<Compile Include="Scenario5.cs" />
<Compile Include="Scenario4.cs" />
<Compile Include="Scenario3.cs" />
<Compile Include="Scenario2.cs" />
<Compile Include="Scenario1.cs" />
<Compile Include="Extensions.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2A84DC12-471E-492F-B390-DF3ADB452E28}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>MethodRedirect</RootNamespace>
<AssemblyName>MethodRedirect</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<StartupObject>
</StartupObject>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Optimize>false</Optimize>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>false</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<UseVSHostingProcess>true</UseVSHostingProcess>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ClassMemberInfo.cs" />
<Compile Include="OriginalMethodsInfo.cs" />
<Compile Include="MethodToken.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MethodUtil.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
12 changes: 7 additions & 5 deletions Sources/MethodToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@ namespace MethodRedirect
public struct MethodToken : IDisposable
{
public IntPtr Address { get; private set; }
public IntPtr Value { get; private set; }
public IntPtr OriginalValue { get; private set; }
public IntPtr TargetValue { get; private set; }

public MethodToken(IntPtr address)
public MethodToken(IntPtr address, IntPtr targetValue)
{
// On token creation, preserve the address and the current value at this address
Address = address;
Value = Marshal.ReadIntPtr(address);
OriginalValue = Marshal.ReadIntPtr(address);
TargetValue = targetValue;
}

public void Restore()
{
// Restore the value at the address
Marshal.Copy(new IntPtr[] { Value }, 0, Address, 1);
Marshal.Copy(new IntPtr[] { OriginalValue }, 0, Address, 1);
}

public override string ToString()
{
IntPtr met = Address;
IntPtr tar = Marshal.ReadIntPtr(Address);
IntPtr ori = Value;
IntPtr ori = OriginalValue;

return "Method address = " + met.ToString("x").PadLeft(8, '0') + "\n" +
"Target address = " + tar.ToString("x").PadLeft(8, '0') + "\n" +
Expand Down
Loading