Created
May 3, 2012 14:08
-
-
Save hmemcpy/2585890 to your computer and use it in GitHub Desktop.
Which do you like better?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #1 | |
public IModule GetTargetModule(ICSharpExpression expression) | |
{ | |
var referenceExpression = expression as IReferenceExpression; | |
if (referenceExpression != null) | |
{ | |
var typeofExpression = referenceExpression.QualifierExpression as ITypeofExpression; | |
if (typeofExpression != null) | |
{ | |
IDeclaredType declaredType = typeofExpression.ArgumentType.GetScalarType(); | |
if (declaredType != null) | |
{ | |
ITypeElement typeElement = declaredType.GetTypeElement(); | |
if (typeElement != null) | |
{ | |
return typeElement.Module.ContainingProjectModule; | |
} | |
} | |
} | |
} | |
return null; | |
} | |
// or #2 | |
public IModule GetTargetModule(ICSharpExpression expression) | |
{ | |
// see http://devtalk.net/csharp/chained-null-checks-and-the-maybe-monad/ | |
return this.With(x => expression as IReferenceExpression) | |
.With(x => x.QualifierExpression as ITypeofExpression) | |
.With(x => x.ArgumentType.GetScalarType()) | |
.With(x => x.GetTypeElement()) | |
.Return(x => x.Module.ContainingProjectModule, null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment