Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
Initial commit of Lambdanator
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed Apr 13, 2011
0 parents commit f1e2554
Show file tree
Hide file tree
Showing 10 changed files with 380 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
@@ -0,0 +1,15 @@
build/
dist/
**/*.suo
**/*.user
bin
obj
_ReSharper*
*.csproj.user
*.suo
*.cache
*.user
*~
*.swp
*.bak
TestResult.xml
76 changes: 76 additions & 0 deletions Lambda.cs
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2011, James Gregory
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the distribution.
* * Neither the name of Lambdanator, James Gregory nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

using System;
using System.Linq.Expressions;
using System.Reflection;

namespace Lambdanator
{
public class λ
{
public static MemberInfo Reflect(Expression<Func<object>> expression)
{
return Reflect(expression.Body);
}

public static MemberInfo Reflect<T>(Expression<Func<T, object>> expression)
{
return Reflect(expression.Body);
}

public static MemberInfo Reflect(Expression<Action> expression)
{
return Reflect(expression.Body);
}

public static MemberInfo Reflect<T>(Expression<Action<T>> expression)
{
return Reflect(expression.Body);
}

public static MemberInfo Reflect(Expression expression)
{
var memberAccess = expression as MemberExpression;

if (memberAccess != null)
return memberAccess.Member;

var unary = expression as UnaryExpression;

if (unary != null)
return Reflect(unary.Operand);

var call = expression as MethodCallExpression;

if (call != null)
return call.Method;

throw new ArgumentException("Not a member access", "expression");
}
}

public class Lambda : λ
{}
}
93 changes: 93 additions & 0 deletions Tests.cs
@@ -0,0 +1,93 @@
using Lambdanator;
using NUnit.Framework;

namespace Tests
{
[TestFixture]
public class Tests
{
string LocalProperty { get; set; }

class Target
{
public int field;
public string Property { get; set; }
public void Method() { }
public object AnotherMethod() { return null; }

public static void StaticMethod() {}
}

[Test]
public void Method_test()
{
var member = λ.Reflect<Target>(x => x.Method());

Assert.AreEqual("Method", member.Name);
}

[Test]
public void Void_method_test()
{
var member = λ.Reflect<Target>(x => x.AnotherMethod());

Assert.AreEqual("AnotherMethod", member.Name);
}

[Test]
public void Local_method_test()
{
var member = λ.Reflect(() => Local_method_test());

Assert.AreEqual("Local_method_test", member.Name);
}

[Test]
public void Static_method_test()
{
var member = λ.Reflect(() => Target.StaticMethod());

Assert.AreEqual("StaticMethod", member.Name);
}

[Test]
public void Property_test()
{
var member = λ.Reflect<Target>(x => x.Property);

Assert.AreEqual("Property", member.Name);
}

[Test]
public void Local_property_test()
{
var member = λ.Reflect(() => LocalProperty);

Assert.AreEqual("LocalProperty", member.Name);
}

[Test]
public void Property_with_cast_test()
{
var member = λ.Reflect<Target>(x => (object)x.Property);

Assert.AreEqual("Property", member.Name);
}

[Test]
public void Field_test()
{
var member = λ.Reflect<Target>(x => x.field);

Assert.AreEqual("field", member.Name);
}

[Test]
public void Field_with_cast_test()
{
var member = λ.Reflect<Target>(x => (object)x.field);

Assert.AreEqual("field", member.Name);
}
}
}
Binary file added lib/nunit.framework.dll
Binary file not shown.
26 changes: 26 additions & 0 deletions projects/Lambdanator.sln
@@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lambdanator", "Lambdanator\Lambdanator.csproj", "{BC95BBA5-1A06-41F8-A2FE-77ACEB1B8B65}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{4D3609E6-9650-4F3B-AB5C-2C6FB477D12E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BC95BBA5-1A06-41F8-A2FE-77ACEB1B8B65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BC95BBA5-1A06-41F8-A2FE-77ACEB1B8B65}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC95BBA5-1A06-41F8-A2FE-77ACEB1B8B65}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC95BBA5-1A06-41F8-A2FE-77ACEB1B8B65}.Release|Any CPU.Build.0 = Release|Any CPU
{4D3609E6-9650-4F3B-AB5C-2C6FB477D12E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4D3609E6-9650-4F3B-AB5C-2C6FB477D12E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D3609E6-9650-4F3B-AB5C-2C6FB477D12E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D3609E6-9650-4F3B-AB5C-2C6FB477D12E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
55 changes: 55 additions & 0 deletions projects/Lambdanator/Lambdanator.csproj
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{BC95BBA5-1A06-41F8-A2FE-77ACEB1B8B65}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Lambdanator</RootNamespace>
<AssemblyName>Lambdanator</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\Lambda.cs">
<Link>Lambda.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
7 changes: 7 additions & 0 deletions projects/Lambdanator/Properties/AssemblyInfo.cs
@@ -0,0 +1,7 @@
using System.Reflection;

[assembly: AssemblyTitle("Lambdaator")]
[assembly: AssemblyProduct("Lambdaator")]
[assembly: AssemblyCopyright("Copyright © James Gregory 2011")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
7 changes: 7 additions & 0 deletions projects/Tests/Properties/AssemblyInfo.cs
@@ -0,0 +1,7 @@
using System.Reflection;

[assembly: AssemblyTitle("Lambdaator Tests")]
[assembly: AssemblyProduct("Lambdaator")]
[assembly: AssemblyCopyright("Copyright © James Gregory 2011")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
65 changes: 65 additions & 0 deletions projects/Tests/Tests.csproj
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{4D3609E6-9650-4F3B-AB5C-2C6FB477D12E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Tests</RootNamespace>
<AssemblyName>Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\..\lib\nunit.framework.dll</HintPath>
</Reference>
<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="..\..\Tests.cs">
<Link>Tests.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Lambdanator\Lambdanator.csproj">
<Project>{BC95BBA5-1A06-41F8-A2FE-77ACEB1B8B65}</Project>
<Name>Lambdanator</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions readme.md
@@ -0,0 +1,36 @@
Lambdanator (λ)
===============

A teeny-tiny static reflection class.


Usage
-----

Just copy [Lambda.cs](/jagregory/lambdanator/blob/master/Lambda.cs) into your project and you're away!

You can clone this repository and build it from source if you want an assembly, or if you want to run the tests.


Examples
--------

λ.Reflect<SomeClass>(x => x.AMethod());

λ.Reflect<SomeClass>(x => x.AProperty);

λ.Reflect(() => local);

If you don't like the `λ` syntax, you can use `Lambda` instead.

Lambda.Reflect<SomeClass>(x => x.AMethod());

Lambda.Reflect<SomeClass>(x => x.AProperty);

Lambda.Reflect(() => local);


License
-------

New BSD, leave it at the top of the file, but otherwise do as you please just don't blame me if it blows up.

0 comments on commit f1e2554

Please sign in to comment.