Created
April 21, 2010 19:51
-
-
Save bamboo/374314 to your computer and use it in GitHub Desktop.
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
import Mono.Cecil | |
import Mono.Cecil.Cil | |
import System | |
import System.Collections | |
import Boo.Lang.PatternMatching | |
def CreateApiStub(fromAssembly as string, toAssembly as string): | |
module = ModuleDefinition.ReadModule(fromAssembly) | |
notImplementedExceptionCtor = module.Import(typeof(System.NotImplementedException).GetConstructor(array[of System.Type](0))) | |
types = module.Types | |
for type in types: | |
RemoveNonPublicMembersFrom(type.NestedTypes) | |
RemoveNonPublicMembersFrom(type.Fields) | |
RemoveNonPublicMembersFrom(type.Properties) | |
RemoveNonPublicMembersFrom(type.Methods) | |
for method in type.Methods: | |
if not method.HasBody: | |
continue | |
method.Body = MethodBody(method) | |
il = method.Body.GetILProcessor() | |
il.Emit(OpCodes.Newobj, notImplementedExceptionCtor) | |
il.Emit(OpCodes.Throw) | |
RemoveNonPublicMembersFrom(types) | |
module.Write(toAssembly) | |
def RemoveNonPublicMembersFrom(collection as IList): | |
i = 0 | |
while i < len(collection): | |
current = collection[i] | |
if IsPublicMember(current): | |
++i | |
continue | |
print "Removing", current.ToString() | |
collection.RemoveAt(i) | |
def IsPublicMember(m) as bool: | |
match m: | |
case PropertyDefinition(GetMethod, SetMethod): | |
return GetMethod is not null and IsPublicMember(GetMethod) \ | |
or SetMethod is not null and IsPublicMember(SetMethod) | |
case TypeDefinition(IsPublic, IsNestedPublic, IsNestedFamily): | |
return IsPublic or IsNestedPublic or IsNestedFamily | |
case MethodDefinition(IsPublic, IsFamily): | |
return IsPublic or IsFamily | |
case FieldDefinition(IsPublic, IsFamily): | |
return IsPublic or IsFamily | |
input, output = argv | |
CreateApiStub(input, output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment