Retrieve type from a String - c#

I have this class:
namespace Ns1.Ns2.Domain
{
public class Process
{
private IIndex IndexBuilderConcr { get; set; }
public Processo(String processType) {
IndexBuilderConcr = new UnityContainer().RegisterType<IIndex, *>().Resolve<IIndex>();
}
}
}
Here I have a constructor that takes a String. This string represent a type that should replace the * sign at line 8.
I have googled araound but with no luck.

What you'll need to do is get the type in the way James S suggests, but you'll need to pass that value into the method in a slightly different way as calling Method<resolvedProcessType> is invalid:
var type = Type.GetType("Some.Name.Space." + processType);
var methodInfo = typeof(UnityContainer).GetMethod("RegisterType");
// this method's argument is params Type[] so just keep adding commas
// for each <,,,>
var method = methodInfo.MakeGenericMethod(IIndex, type);
// we supply null as the second argument because the method has no parameters
unityContainer = (UnityContainer)method.Invoke(unityContainer, null);
unityContainer.Resolve<IIndex>();

The dav_i answer is pretty correct, indeed, since I'm using Unity the best way to achieve this is named mapping:
namespace Ns1.Ns2.Domain
{
public class Process
{
private IIndex IndexBuilderConcr { get; set; }
public Processo(String processType) {
IndexBuilderConcr = new UnityContainer().Resolve<IIndex>(processType);
}
}
}
and then inside the App.config:
<container>
<register type="IIndex" mapTo="IndexAImpl" name="A"/>
<register type="IIndex" mapTo="IndexBImpl" name="B"/>
</container>
where name attributes is the processType string.

The method for this is the static Type.GetType(fullyQualifiedTypeNameString) You need to know the namespace for this to be possible, but the assembly name is inferred if it is in the currently executing assembly.
So if the namespace is known, but not passed in you could do
string fullyQualifiedTypeName = "MyNamespace." + processType;
Type resolvedProcessType = Type.GetType(fullyQualifiedTypeName);
However you can't just pass this Type object as a typeparameter, because that's not how generics work. Generics require the type to be known at compile time - so passing a Type object as a Typeparameter is not a valid way of saying that is the required Type.
To get round this reflection can be used instead. There is another answer by dav_i which demonstrates this.

Related

Reflection using generics and late-binding. How to cast at run-time?

I am trying to use Generics with Reflection in c# to build a method that can handle multiple classes. I use a 3rd-party DLL that has a bunch of classes and on those classes, there is a method I call. They all return different return types, but I do the same processing once I get the object back (in my example below, that would be AreaA and AreaB).
Basically, I want to develop a method that takes in the class name and the expected return type as Generic variables and then calls the correct method (methodName) which is supplied as a parameter to this method.
The program below compiles fine and runs without error, but the issue is the expected type of the 'area' variable. In the below statements, the first line is type casted to (TArea), and if I hover over it In Visual Studio, the intellisense shows the property 'name', but typing area.name doesn't give me the value. I have to type ((AreaA)area).name.
Problem is the type 'AreaA' could be another type at run-time. In this example, 'AreaB' so I can hard-code a cast.
How can I accomplish casting the 'area' variable to the appropriate type allowing me to see the public methods/properties of the 3rd-parties class?
NOTE: In my example, eveything is in the same class, but in reality the definitions for ServiceA, ServiceB, AreaA and AreaB will be in a 3rd party DLL.
As always, thanks in advance!
Fig 1 - 'area' variable can only get 'name' property if casted to 'AreaA'
area = (TArea)dfpMethod.Invoke(instance, new object[] { "Area123" });
AreaA areaa = (AreaA)dfpMethod.Invoke(instance, new object[] { "Area123" });
Fig 2. - Complete Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
namespace ReflectionExample
{
class Sample
{
class ServiceA
{
public int size {get; set;}
public string name {get; set;}
public ServiceA()
{
name = "TestA";
size = 100;
}
public AreaA doWork(string name)
{
return new AreaA(name);
}
}
class AreaA
{
public string name { get; set;}
public AreaA(string name)
{
this.name = name;
}
public AreaA()
{
}
}
class ServiceB
{
public int size { get; set; }
public string name { get; set; }
public ServiceB()
{
name = "TestB";
size = 50;
}
public AreaB doWork(string name)
{
return new AreaB(name);
}
}
class AreaB
{
public string name { get; set; }
public AreaB(string name)
{
this.name = name;
}
public AreaB()
{
}
}
static void Main(string[] args)
{
runService<ServiceA, AreaA>("doWork");
}
private static void runService<TService, TArea>(string methodName)
where TService : class, new()
where TArea : class, new()
{
//Compile time processing
Type areaType = typeof(TArea);
Type serviceType = typeof(TService);
//Print the full assembly name and qualified assembly name
Console.WriteLine("AreaType--Full assembly name:\t {0}.", areaType.Assembly.FullName.ToString()); // Print the full assembly name.
Console.WriteLine("AreaType--Qualified assembly name:\t {0}.", areaType.AssemblyQualifiedName.ToString()); // Print the qualified assembly name.
Console.WriteLine("ServiceType--Full assembly name:\t {0}.", serviceType.Assembly.FullName.ToString()); // Print the full assembly name.
Console.WriteLine("ServiceType--Qualified assembly name:\t {0}.", serviceType.AssemblyQualifiedName.ToString()); // Print the qualified assembly name.
//This is done because in my code, the assembly doesn't reside in the executiy assembly, it is only setup as a reference
var assembly = Assembly.Load(serviceType.Assembly.FullName);
//Initialize the generic area
TArea area = default(TArea);
//Get an instance of the service so I can invoke the method later on
var instance = Activator.CreateInstance(serviceType);
//Get the methodInfo for the methodName supplied to the runService method
MethodInfo dfpMethod = serviceType.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
//area is type casted to (TArea), the intellisense shows the property 'name', but typing area.name doesn't give me the value
//I have to type ((AreaA)area).name. Problem is the type 'AreaA' could be another type. In this example, 'AreaB'
area = (TArea)dfpMethod.Invoke(instance, new object[] { "Area123" });
AreaA areaa = (AreaA)dfpMethod.Invoke(instance, new object[] { "Area123" });
Console.WriteLine();
}
}
}
The source of your error is that you're casting all of your return values to type TArea with the statement:
TArea area = (TArea)dfpMethod.Invoke(instance, new object[] { "Area123" });
Per your generic specification, the only thing promised to you by the type TArea is that its a class. Therefore, TArea doesn't give you access to anything but members of the 'object' type.
Instead, do away with the TArea generic argument in favor of using the 'dynamic' keyword:
var area = (dynamic)dfpMethod.Invoke(instance, new object[] { "Area123" });
return area.name; // no error
Note, this is only relevant if the actual types AreaA and AreaB are defined in third party libraries (as you say) and you can't modify them. If you can't modify the classes, you can't introduce an interface (which is what you really need here). If you can't introduce an interface, but all the types expose identical signatures, you can assume the existence of the relevant members using the dynamic type.
If you need to do a lot of work with AreaA/AreaB and you don't want the performance overhead of all the dynamic operations, define your own generalized class that exposes all the signatures you need:
public class MyGenericArea
{
public MyGenericArea(string name)
{
this.Name = name;
}
public string Name {get; set;}
}
Then populate the class using the dynamic casting and return that class type instead:
var area = (dynamic)dfpMethod.Invoke(instance, new object[] { "Area123" });
return new MyGenericArea(area.name);
I think the problem you will have here is mixing dynamically loaded types (Assembly.Load()) with types that are referenced directly from your project and you can see in intellisense, i.e. AreaA.
If you are dynamically loading entire assemblies using reflection, intellisense will do nothing to help you view the class members, as that information needs to be known at compile time, and by definition you are loading assemblies at runtime.
If you just want to view a list of all of the public properties available to your type, then you can use this:
var areaProperties = area.GetType().GetProperties();
But again, this is all done at run time so it wont help you when writing code.
You can dynamically read the value of the "name" property using:
var nameValue = area.GetType().GetProperty("name").GetValue(area);
Essentially, if you want intellisense, reference the dll's directly from your Visual Studio project rather than using Assembly.Load().
Hope that helps.

Can I access a class variable with another variable?

i want to do a class constructor that takes a dicionary as parameter and initialize all the class variables that are listed as key in the dictionary, after of course a type conversion:
public class User
{
public int id;
public string username;
public string password;
public string email;
public int mana_fire;
public int mana_water;
public int mana_earth;
public int mana_life;
public int mana_death;
public User ()
{
}
public User(Dictionary<string,string> dataArray){
FieldInfo[] classVariablesInfoList = typeof(User).GetFields();
for(int i = 0; i < classVariablesInfoList.Length; i++)
{
if(dataArray.ContainsKey(classVariablesInfoList[i].Name)){
//missing code here :)
//need something like classVariable= dataArray["classVariablesInfolist[i].name"]; ?
}
}
}
}
but i can't find out how to do this!
Can you please help? :)
You can use the SetValue frunction from reflection:
FieldInfo f = classVariablesInfoList[i];
if (f.ReflectedType == typeof(int))
{
f.SetValue(this, Convert.ToInt32(dataArray[f.Name]));
}
else
{
f.SetValue(this, dataArray[classVariablesInfoList[i].Name]);
}
But it is a really uncommon way to do this with a dictionary. You should considder accessing the fields directly or add parameters to the constructor for any field. And fields should never be public - use properties instead.
The following will work if Convert.ChangeType() is able to handle the conversion. There are a lot of problems waiting to occur, for example handling numbers or dates where the string representation depends on the locale. I would really suggest to use usual typed constructor parameters or standard (de)serialization mechanism if possible. Or at least use a dictionary containing objects instead of strings to get rid of the conversion, again if possible.
public User(Dictionary<String, String> data)
{
var fields = typeof(User).GetFields();
foreach (field in fields)
{
if (data.ContainsKey(field.Name))
{
var value = Convert.ChangeType(data[field.Name], field.MemberType);
field.SetValue(this, value);
}
}
}
I would like to separate your problem into two parts.
1. Applying conversion
The FieldInfo type present a FieldType property that is the actual type of the field, using this Type we can use the non-generic ChangeType method of System.Convert, this method will be able convert some types to others. Luckily it support String to Int.
Usage:
Convert.ChangeType(OLD_VALUE, TARGET_TYPE);
2. Setting the field
The field info class has a SetValue method (FieldInfo.SetValue), it has two parameters, the first one is the current (ie. this) instance (or the instance you wish to change). the second is the new value you wish to set.
Putting it all together
[...]
var fieldInfo = classVariablesInfoList[i];
var name = fieldInfo.Name;
var targetType = fieldInfo.Type;
var value = Convert.ChangeType(dataArray[name], targetType);
classVariablesInfoList[i].SetValue(this, value);

The type arguments for method cannot be inferred from the usage error for creating a relationship

So I have a class that describes the Relationship like so:
public class GraphRelationship<TObject> : Relationship<RelationshipObject>, IRelationshipAllowingSourceNode<TObject>, IRelationshipAllowingTargetNode<TObject>
{
string RelationshipName;
public GraphRelationship(string RelationshipName, NodeReference targetNode, RelationshipObject relationshipTypeObject)
: base(targetNode, relationshipTypeObject)
{
this.RelationshipName = RelationshipName;
}
public override string RelationshipTypeKey
{
get { return RelationshipName; }
}
}
Now I have a method that I want to use to create an instance of the above mentioned class, but I get this The type arguments for method cannot be inferred from the usage error.
Here is the method:
public RelationshipReference CreateObjectRelationship(string relationshipType, string parentObjectId, string childObjectId, RelationshipObject relationshipProperties)
{
RelationshipReference relationshipReference = 0;
NodeReference parentNodeReference = GetObjectReference(parentObjectId);
NodeReference childNodeReference = GetObjectReference(childObjectId);
//This is where the error is
relationshipReference = GraphConnection.CreateRelationship(parentNodeReference, new GraphRelationship<RelationshipObject>(relationshipType, childNodeReference, relationshipProperties));
return relationshipReference;
}
Im sure this is a trivial problem, but how would i fix this?
So I fixed it, my NodeReferences needed to be of Type <TObject>
NodeReference<TObject> parentObjectReference = GetObjectReference(Id);
It looks like you're trying to make a generic implementation of a generic relationship, with an obsolete part of the Neo4jClient API.
Use Cypher. ;)

Resolve instance with multiple constructors using unity

I'd like to create an instance of a class using unity where the class has two constructors with the same number of parameters.
Here is the instantiation:
_unityContainer.Resolve<IGradeType>(new ParameterOverride("gradeTypeStringFromXmlFile", gradeTypeStringFromXmlFile));
And here are the constructors:
public GradeType(string gradeTypeStringFromXmlFile)
{
_gradeTypeStringFromXmlFile = gradeTypeStringFromXmlFile;
}
public GradeType(Enum.GradeType gradeType)
{
_gradeType = gradeType;
}
If I try to do this I get an exception saying The type GradeType has multiple constructors of length 1. Unable to disambiguate.
I can set the attribute [InjectionConstructor] over one constructor to make it work with one, but then I can't create an instance with unity using the other constructor.
Is it some way to have multiple constructors with equal number of parameters and still use unity to create the instances?
Yes it's possible to tell Unity which constructor should it use, but you can only do this when you register your type with InjectionConstructor. If you want to use both constructor it's even complicated because you have to name your registrations and use that name when resolving.
Sample built with Unity version 2.1.505:
var continer = new UnityContainer();
continer.RegisterType<IGradeType, GradeType>("stringConstructor",
new InjectionConstructor(typeof(string)));
continer.RegisterType<IGradeType, GradeType>("enumConstructor",
new InjectionConstructor(typeof(EnumGradeType)));
IGradeType stringGradeType = continer.Resolve<IGradeType>("stringContructor" ,
new DependencyOverride(typeof(string), "some string"));
IGradeType enumGradeType = continer.Resolve<IGradeType>("enumConstructor",
new DependencyOverride(typeof(EnumGradeType), EnumGradeType.Value));
An alternative option using Reflection and following the Strategy Pattern.
1) Create a base class for the constructors' arguments
public abstract class ConstructorArgs
{
}
2) Create a sequence of different concrete arguments classes:
public class StringArg : ConstructorArgs
{
public string _gradeTypeStringFromXmlFile { get; set; }
public StringArg (string gradeTypeStringFromXmlFile)
{
this._gradeTypeStringFromXmlFile = gradeTypeStringFromXmlFile ;
}
}
public class EnumArg : ConstructorArgs
{
public Enum.GradeType _gradeType { get; set; }
public EnumArg (Enum.GradeType gradeType)
{
this._gradeType = gradeType ;
}
}
3) Now in your GradeType class create the methods required for the Reflection. The ParseArguments scans the args for properties and for each one that it finds, it copies its value to the respective property of the GradeType using the SetProperty. Since it uses the property name for the matching, it is important to keep the same property name across both the GradeType and the concrete ConstructorArgs:
private void SetProperty(String propertyName, object value)
{
var property = this.GetType().GetProperty(propertyName);
if (property != null)
property.SetValue(this, value);
}
private void ParseArguments(ConstructorArgs args)
{
var properties = args.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in properties)
{
this.SetProperty(propertyInfo.Name,
args.GetType().GetProperty(propertyInfo.Name).GetValue(args));
}
}
4) In your GradeType class create the respective properties (mind that you must use exactly the same names and types that you used in the concrete ConstructorArgs but you can use any access modifiers you like)
public string _gradeTypeStringFromXmlFile { get; set; }
public Enum.GradeType _gradeType { get; set; }
5) Create a constructor for your GradeType class with a parameter of type ConstructorArgs:
public GradeType(ConstructorArgs args)
{
this.ParseArguments(args);
}
6) Now you can register the GradeType in Unity using a single constructor but you can pass in different types as arguments when resolving it:
_unityContainer.RegisterType<IGradeType, GradeType>(
new InjectionConstructor( typeof(ConstructorArgs) ));
var args1 = new StringArg(gradeTypeStringFromXmlFile); // string
IGradeType gradeType1 = _unityContainer.Resolve<IGradeType>(
new ResolverOverride[]{new ParameterOverride("args", args1)});
var args2 = new EnumArg(gradeType); // enum
IGradeType gradeType2 = _unityContainer.Resolve<IGradeType>(
new ResolverOverride[]{new ParameterOverride("args", args2)});
If you are planning to repeatedly resolve your type in an iteration that approach might not be ideal, since Reflection comes with a performance penalty.
Remove one constructor, and cast the string to the enum, or vice-versa, and then resolve using the container.

What's the return type of an anonymous class

I have a class that used to have a string return type. Now I find I need to return more than a string. I was thinking to return something like below:
public string Test()
{
return ( new { ID = 5, Name= "Dave" } );
}
Is this even possible and if so then what would be the return type? I know it's not string ..
As others have said, the best thing to do here is to make a nominal type. I would suggest that the nominal type have the same characteristics as an anonymous type; that is, you should consider making the type immutable and consider making it exhibit value equality.
It is possible to return an anonymous type as object and then use the instance returned elsewhere using a variety of sneaky techniques. You can cast the object to "dynamic" (in C# 4) and then use the properties of the anonymous type, but this is slow and lacks compile-time type checking.
You can also use the "cast by example" trick, which does get you compile-time type checking. However, that trick only works when the anonymous source object and the anonymous example object come from the same assembly.
static T CastByExample<T>(object source, T example) where T : class
{
return source as T;
}
static object ReturnsAnonymous() { return new { X = 123 }; }
static void DoIt()
{
object obj = ReturnsAnonymous();
var example = new { X = 0 };
var anon = CastByExample(obj, example);
Console.WriteLine(anon.X); // 123
}
See how sneaky that is? We use method type inference and local variable type inference to tell the compiler "these two things are the same type". This lets you export an anonymous type as object and cast it back to anonymous type.
But you probably should not do this; if you're resorting to such sneaky tricks then you should simply be defining a nominal type in the first place. Also, like I said, the trick only works if the example and the source objects were created in code in the same assembly; two "identical" anonymous types in two different assemblies do not unify to be the same type.
The object that you return does have a class, but it's anonymous so you can't specify it in the code. You just have to return it as an object reference:
public object Test() {
return new { ID = 5, Name= "Dave" };
}
Note that the anonymous type is unknown outside the scope of the method, so reflection is the only way to access its properties.
If you want to be able to use the returned object conveniently, you should declare a class:
public class TestResult
{
public int ID { get; set; }
public string Name { get; set; }
}
public TestResult Test() {
return new TestResult() { ID = 5, Name= "Dave" };
}
Another alternative is to use an existing class, if it fits your purpose. A KeyValuePair is close to what you use, but then the properties will of course be named Key and Value instead of ID and Name:
public KeyValuePair<int, string> Test() {
return new KeyValuePair<int, string>(5, "Dave");
}
This isn't possible as the anonymous class is only valid within the current context. If you need to return an object then you'll need to create a real class.
I'm assuming you left string as the return type by accident.
Anonymous type are class type that are derived directly from object.
You can return it from method as object as return type.
Have a look at this.
No, it's not possible. Your options are:
Define a real class for the return value,
Use System.Tuple, or
Use out parameters (probably the least good option).
You can make a struct (or class) for this.
public struct IdAndName
{
public int Id;
public string Name;
public IdAndName(int id, string name)
{
ID = id;
Name = name;
}
}
You could also use a Tuple<T1, T2>, (but that's not recommended as the properties aren't named.
class NewString
{
public int ID { get; set; }
public string Name { get; set; }
}
public NewString Test()
{
return ( new NewString() { ID = 5, Name = "Dave" } );
}
:)

Categories