Skip to content

Instantly share code, notes, and snippets.

@DinisCruz
Created June 19, 2012 01:35
Show Gist options
  • Select an option

  • Save DinisCruz/2951822 to your computer and use it in GitHub Desktop.

Select an option

Save DinisCruz/2951822 to your computer and use it in GitHub Desktop.
O2 Scripts that Show type Confusion in Action
var textBox = panel.add_TextBox(true);
textBox.set_Text("hello world");
var label = (Label)textBox.castViaTypeConfusion<Label>();
return label.FlatStyle.str(); // will return a value that depends on some part of the TextBox object raw value
//return textBox.FlatStyle; // doesn't compile: System.Windows.Forms.TextBox' does not contain a definition for 'FlatStyle' and no extension method 'FlatStyle' accepting a first argument of type
//O2File:_Extra_methods_TypeConfusion.cs
public static string create_DLL_TO_castViaTypeConfusion()
{
var assemblyName = "TypeConfusion";
var dllName = assemblyName + ".dll";
var appDomain = AppDomain.CurrentDomain;
var targetDir = "".tempDir(false);
var targetFile = targetDir.pathCombine(dllName);
if (targetFile.fileExists())
return targetFile;
var assemblyBuilder = assemblyName.assemblyBuilder_forSave(targetDir); // use this if wanting to Save the assembly created
var moduleBuilder = assemblyBuilder.dynamicModule(dllName);
var typeBuilder = moduleBuilder.dynamicType(assemblyName);
var methodBuilder = typeBuilder.dynamicMethod("castObjectIntoType", null, typeof(object));
var genericParameters = methodBuilder.DefineGenericParameters("T");
var returnType = genericParameters[0];
methodBuilder.SetReturnType(returnType);
var ilGenerator = methodBuilder.il();
ilGenerator.DeclareLocal(typeof(object));
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Stloc_0);
ilGenerator.Emit(OpCodes.Ldloc_0);
ilGenerator.ret();
var type = typeBuilder.create();
assemblyBuilder.Save(dllName);
return targetFile;
}
//O2File:DynamicTypes.cs
//O2Tag_DontAddExtraO2Files
//O2Ref:TypeConfusion.dll
namespace O2.XRules.Database.Utils
{
public static class _extra_TypeConfusion_ExtensionMethods
{
public static T castViaTypeConfusion<T>(this object _objectToCast)
{
return (T)new TypeConfusion().castObjectIntoType<T>(_objectToCast);
}
string aString = "1234";
var aStringBuilder = (StringBuilder)aString.castViaTypeConfusion<StringBuilder>();
//the next line will trick the compiler (and runtime engine) into thinking that the object returned by castViaTypeConfusion is a StringBuilder
return aStringBuilder; // returns "1234"
return aStringBuilder.Equals(aString); // returns true
//other examples:
// aStringBuilder == aString; // won't compile due to : Operator '==' cannot be applied to operands of type 'System.Text.StringBuilder' and 'string'
//var aStringBuilder = (String)aString.castViaTypeConfusion<StringBuilder>(); // won't compile due to : Cannot convert type 'System.Text.StringBuilder' to 'string'
aStringBuilder = aString.castViaTypeConfusion<StringBuilder>(); // the cast will be done to the value of the generic type
try
{
aStringBuilder.AppendLine("12"); // this will compile since the compiler thinks that aStringBuilder is a StringBuilder
}
catch(Exception ex)
{
ex.logStackTrace(); // catches error:
// at System.Text.StringBuilder.Append(String value)
// at System.Text.StringBuilder.AppendLine(String value)
// at DynamicType.dynamicMethod(Panel panel) in
}
//Int32.Parse(aStringBuilder); // doesn't compile
var aNumber = Int32.Parse((string)(object)(aStringBuilder));
return aNumber; // returns the int: 1234
//here is another way to convert into int
return ((string)(object)(aStringBuilder)).toInt(); // returns the int: 1234
//doing the cast without using the castViaTypeConfusion extension method:
var sbBuilder = (StringBuilder)new TypeConfusion().castObjectIntoType<StringBuilder>(aString);
//using System.Text
//O2Ref:TypeConfusion.dll
//_O2Tag_DontAddExtraO2Files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment