Thursday, February 27, 2014

CSharp C# Simple example of changing an objects variables with Reflection

CSharp C# Simple example of changing an objects variables with Reflection





using System;

namespace ReflectVariable
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate object with initial values:
            ReflectVariableTest ReflectVariableTestNow = new ReflectVariableTest();
            ReflectVariableTestNow.MyString = "The Initial Value";
            ReflectVariableTestNow.MyInt = 1;
           
            //Console out the values set initially:
            Console.WriteLine("ReflectVariableTestNow.MyString (A) ={0}", ReflectVariableTestNow.MyString);
            Console.WriteLine("ReflectVariableTestNow.MyInt(A) ={0}", ReflectVariableTestNow.MyInt);
           
            //Change the two values:
            UpdateValueOfStringValue("The New Value", ReflectVariableTestNow, "MyString");
            UpdateValueOfIntValue(2, ReflectVariableTestNow, "MyInt");
           
            Console.WriteLine();
           
            //Console out the values as changed:
            Console.WriteLine("ReflectVariableTestNow.MyString (B) ={0}", ReflectVariableTestNow.MyString);
            Console.WriteLine("ReflectVariableTestNow.MyInt(B) ={0}", ReflectVariableTestNow.MyInt);
           
            Console.WriteLine("Done.  Hit the enter key");
            Console.ReadLine();
        }

        public static void UpdateValueOfStringValue(string ValueNow, object ClassToReflect, string FieldNameNow)
        {
            Type TypeNow = ClassToReflect.GetType();         
            System.Reflection.FieldInfo FieldToModify = TypeNow.GetField(FieldNameNow);
            FieldToModify.SetValue(ClassToReflect, ValueNow);
        }

        public static void UpdateValueOfIntValue(int ValueNow, object ClassToReflect, string FieldNameNow)
        {
            Type TypeNow = ClassToReflect.GetType();
            System.Reflection.FieldInfo FieldToModify = TypeNow.GetField(FieldNameNow);
            FieldToModify.SetValue(ClassToReflect, ValueNow);
        }
    }

    public class ReflectVariableTest
    {
        public string MyString//Note that {get; set;} makes this not read as a Field
        public int MyInt;
    }
}



Sunday, February 23, 2014

CSharp C# List item dynamically edited

CSharp C# List item dynamically edited

The following is an example of editing an item in a list dynamically





using System;
using System.Collections.Generic;
using System.Management.Instrumentation;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleStuff
{
    class Program
    {
        private static List<myclass> mc;
        static void Main(string[] args)
        {
            //------------------------------------------------------
            mc = new List<myclass>();
            mc.Add(new myclass{sString="fun n games"});
            Console.WriteLine("mc[0].sString={0}", mc[0].sString);
            mc[0].sString = "no longer fun n games now";
            Console.WriteLine("mc[0].sString={0}", mc[0].sString);
            //------------------------------------------------------
            Console.WriteLine("press enter to finish");
            Console.ReadLine();
        }
    }

    public class myclass
    {
        public string sString { get; set; }
    }
}



Wednesday, February 19, 2014

Visual Studio CSharp C# Tasks to track comments as links

Visual Studio CSharp C#  Tasks to track comments as links

Example here uses Visual Studio 2012

Better alternative that Visual Studio Bookmarks.

This is a method to just use certain wording in your code comments and Visual Studio will Track them so you can double-click them in the task window as a link to that area of the code.  You can add your own "customized" prefix and set your instance of Visual Studio to track this.
 
 


How to: Create Task List Comments
Visual Studio 2008
7 out of 9 rated this helpful - Rate this topic
The Task List displays comments in your code that begin with the comment marker for your development language. Next to the comments, the Task List also displays a default task token, such as TODO, HACK, or UNDONE, or a custom comment token. The number of comments that appear in the Task List may change, depending on the type of project you are working on. With Visual Basic and Visual C#, the Task List displays all the comments in the solution. With Visual C++ projects, the Task List displays only the comments that are found in the file that is currently active in the editor.
Task List comments can be used to indicate a variety of work to be done at the location marked, including:
  • features to be added;
  • problems to be corrected;
  • classes to implement;
  • place markers for error-handling code; and
  • reminders to check in the file.
As with other Task List entries, you can double-click any comment entry to display the file indicated in the Code Editor and jump to the line of code marked.