How to Add Reflection to a C++ Application

How can I add reflection to a C++ application?

Ponder is a C++ reflection library, in answer to this question. I considered the options and decided to make my own since I couldn't find one that ticked all my boxes.

Although there are great answers to this question, I don't want to use tonnes of macros, or rely on Boost. Boost is a great library, but there are lots of small bespoke C++0x projects out that are simpler and have faster compile times. There are also advantages to being able to decorate a class externally, like wrapping a C++ library that doesn't (yet?) support C++11. It is fork of CAMP, using C++11, that no longer requires Boost.

How to use reflection to create a reflection machine

Try looking at Crack.NET. It is used to do runtime manipulation and interrogation on WPF/WinForms but the source is available and might be a good start if it already doesn't meet your needs.

Using reflection to add entities

You can try expression trees to do the "magic" stuff.
For example:

class BaseEntity
{

}
class MessageList : BaseEntity
{

}
class Application:BaseEntity
{
public List<BaseEntity> MessageLists { get; set; }
public Application()
{
MessageLists = new List<BaseEntity>();
}
}
class ExpressionHelper
{
public static Action<object,object> GetMethod(string targetTypename,string targetMemberName,string sourceTypeName)
{
ParameterExpression targetExpression = Expression.Parameter(typeof(object));
MemberExpression propertyExpression = Expression.PropertyOrField(Expression.TypeAs(targetExpression, Type.GetType(targetTypename)), targetMemberName);
ParameterExpression sourceExpression = Expression.Parameter(typeof(object));
Expression callExpression = Expression.Call(propertyExpression, "Add", null, Expression.TypeAs(sourceExpression, Type.GetType(sourceTypeName)));

var lambda = Expression.Lambda<Action<object, object>>(callExpression, targetExpression, sourceExpression);
var method = lambda.Compile();
return method;
}
}

Usage:

    var app = new Application();
var messageList = new MessageList();
var magicMethod = ExpressionHelper.GetMethod(/*type*/"Application",/*name of Property-Storage*/"MessageLists",/*type of element to store*/"MessageList");
magicMethod(app, messageList);
//now app.MessageLists has one element

Using reflection to create an instance [duplicate]

You can use Activator.CreateInstance for this scenario.

object person = Activator.CreateInstance(Type.GetType("Human.Person"));

or most commonly using a base class or interface:

IPerson person = (IPerson)Activator.CreateInstance(Type.GetType("Human.Person"));

Accessing external Assembly Reflection

Just google what specifically you are trying to do, pretty much everything you want to do with reflection is on stack overflow in some shape or form...

Assembly asm = Assembly.LoadFrom(@"C:\Sandbox\Functions\Bin\Debug\Functions.dll");

foreach (Type t in asm.GetTypes())
{
//... t.FullName
//... t.GetAllBaseClassesAndInterfaces
//... t.GetNestedTypes
}

https://stackoverflow.com/a/1315668/588734

Given a C# Type, Get its Base Classes and Implemented Interfaces

Load Assembly and create class using Reflection

You need to do two things:

1) Convert your string str into a type, and
2) Create an instance of that type.

string typeString = "My.Assembly.Namesapce.MyClass, My.Assembly.Namesapce, Version=4.0.0.0, Culture=neutral, PublicKeyToken=84474dc3c6524430";

// Get a reference to the type.
Type theType = Type.GetType(typeString, true, false);

// Create an instance of that type.
MyClass theInstance = (MyClass)Activator.CreateInstance(theType);

Reflection: Create instance of a class that inherit from another class located in separate assembly

You load an assembly built in .NET Framework via reflection on .NET Core, but some of the classes in System.Web namespace are not supported in .NET Core. It load user code parts. And other parts may not work. In your case the requested class of the code compiled for .NET Framework is not part of .NET Core.
System.Web is for Asp.Net and in .NET Core the System.Web namespace exists and could technically be called but it won't work like you want.

Adding items to ListT using reflection

You're trying to find an Add method in Type, not in List<MyObject> - and then you're trying to invoke it on a Type.

MakeGenericType returns a type, not an instance of that type. If you want to create an instance, Activator.CreateInstance is usually the way to go. Try this:

Type objTyp = typeof(MyObject); //HardCoded TypeName for demo purpose
var IListRef = typeof (List<>);
Type[] IListParam = {objTyp};
object Result = Activator.CreateInstance(IListRef.MakeGenericType(IListParam));

MyObject objTemp = new MyObject();
Result.GetType().GetMethod("Add").Invoke(Result, new[] {objTemp });

(I would also suggest that you start following conventions for variable names, but that's a separate matter.)



Related Topics



Leave a reply



Submit