Skip to content

Instantly share code, notes, and snippets.

@DinisCruz
DinisCruz / gist:2911656
Created June 11, 2012 18:02
O2 Script - Refactored Roslyn 'AddingMethodToClass' method (smaller version)
var code = @"class C { }";
var classDeclaration = code.astTree()
.compilationUnit()
.classes().first();
classDeclaration.replace(classDeclaration.add("M".methodDeclaration()))
.formatedCode();
@DinisCruz
DinisCruz / gist:2924498
Created June 13, 2012 14:43
O2 Script to Removing an Event from a WinForm control using reflection
//for a UserControl (in fact any control that implements System.ComponentModel.Component)
var userControl = new UserControl();
//we can get the current mapped event handlers
userControl.eventHandlers();
//its signature
userControl.eventHandlers_MethodSignatures();
//remove one by using the static field name
userControl.remove_EventHandler("EVENT_SELECTEDINDEXCHANGED");
//or use this one specifically mapped to the SelectedIndexChanged event
userControl.remove_Event_SelectedIndexChanged
@DinisCruz
DinisCruz / API_Win32Processes_Info.cs
Created June 17, 2012 21:55
O2 Script to view open Process Handles (simpler version)
// This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Text;
using O2.Interfaces.O2Core;
using O2.Kernel;
@DinisCruz
DinisCruz / Script 3 - extensionMethod.cs
Created June 18, 2012 12:39
O2 Script to test GUI Height issue
public static Form clientSize(this Form form, int width, int height)
{
return (Form)form.invokeOnThread(() =>
{
form.ClientSize = new Size(width, height);
return form;
});
}
@DinisCruz
DinisCruz / Casting a TextBox as a Label.cs
Created June 19, 2012 01:35
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
@DinisCruz
DinisCruz / Code executed inside REPL.cs
Created June 19, 2012 03:03
O2 Script used to create the 'Script the Script' PoC
var _script = (script as object).castViaTypeConfusion<ascx_Simple_Script_Editor>();
_script.execute();
return _script.Code;
//O2Tag_SetInvocationParametersToDynamic
//O2Tag_DontUseCachedAssemblyIfAvailable
//O2Tag_DontAddExtraO2Files
//O2File:ascx_Simple_Script_Editor.cs.o2
//O2File:_Extra_methods_TypeConfusion.cs
@DinisCruz
DinisCruz / gist:2952462
Created June 19, 2012 05:37
Roslyn NerdDinner compilation errors (on June 2012)
iCalResult.cs(34,33): error CS0115: 'NerdDinner.Controllers.iCalResult.WriteFile(System.Web.HttpResponseBase)': no suitable method found to override
iCalResult.cs(14,18): error CS0534: 'NerdDinner.Controllers.iCalResult' does not implement inherited abstract member 'System.Web.Mvc.FileResult.WriteFile(System.Web.HttpResponseBase)'
RSSResult.cs(33,33): error CS0115: 'NerdDinner.Controllers.RssResult.WriteFile(System.Web.HttpResponseBase)': no suitable method found to override
RSSResult.cs(12,18): error CS0534: 'NerdDinner.Controllers.RssResult' does not implement inherited abstract member 'System.Web.Mvc.FileResult.WriteFile(System.Web.HttpResponseBase)'
AccountController.cs(70,18): error CS8000: This language feature ('missing type') is not yet implemented in Roslyn.
AccountController.cs(158,31): error CS8000: This language feature ('missing type') is not yet implemented in Roslyn.
DinnersController.cs(39,73): error CS8000: This language feature ('Expression Trees') is not yet implemented in Roslyn.
DinnersCo
@DinisCruz
DinisCruz / gist:3111886
Created July 14, 2012 15:54
C# - Download png, convert to Icon and save to disk
var file = "NodePadIcon.ico".tempFile();
var icon = "http://upload.wikimedia.org/wikipedia/commons/e/ef/Gartoon-Gedit-icon.png".uri().download(false)
.bitmap().asIcon();
using (FileStream fs = new FileStream(file, FileMode.Create))
icon.Save(fs);
panel.add_PictureBox().open(file);
return file;
@DinisCruz
DinisCruz / gist:3136362
Created July 18, 2012 13:57
O2 Script to create a mini NotePad (with only open and save Function)
//var topPanel = "{name}".popupWindow(700,400);
var topPanel = panel.clear().add_Panel();
var textBox = topPanel.add_TextBox(true)
.set_Text("hello world").wordWrap(true);
Action<string> openFile =
(file)=> textBox.set_Text(file.fileContents());
Action saveFile =
()=> textBox.get_Text().saveAs(topPanel.askUserForFileToSave(""));
@DinisCruz
DinisCruz / gist:3185313
Created July 26, 2012 23:59
VisualStudio VSIX: Adding an item to the ErrorList
var vsixPackage = O2_FluentSharp_VSIXPackage.vsixPackage; // this is a reference to an Package object
var ivsSolution = (IVsSolution)Package.GetGlobalService(typeof(IVsSolution));
var dte = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(EnvDTE.DTE));
var errorListProvider = new ErrorListProvider(vsixPackage);
var errorText = "this is a test item";
var errorCategory = TaskErrorCategory.Error;
//Get first project details
var proj = dte.Solution.Projects.Item(1);