One method to read parameters, properties and return types at runtime using C# - dynamic

With continutation to my earlier thread Using reflection read properties of an object containing array of another object. I am hoping to make this wonderful method from EvgK a generic method that can be used in multiple places in my code base.
public static void GetMyProperties(object obj)
{
List<MyPropertyInfo> oMyProp = new List<MyPropertyInfo>();
foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
{
if (!Helper.IsCustomType(pinfo.PropertyType))
{
//add properties - name, value, type to the list
}
else
{
var getMethod = pinfo.GetGetMethod();
if (getMethod.ReturnType.IsArray)
{
var arrayObject = getMethod.Invoke(obj, null);
foreach (object element in (Array)arrayObject)
{
foreach (PropertyInfo arrayObjPinfo in element.GetType().GetProperties())
{
//add properties - name, value, type to the list
}
}
}
else
{
List<MyPropertyInfo> oTempMyProp = GetMyProperties(prop.GetValue(obj, null));
oMyProp.AddRange(oTempMyProp);
}
}
}
}
Again, I am trying to read a method passed by the user. I list the parameters, their properties and values. Once user provides the input values, I call the method dynamically to get the result object. The result is passed to GetMyProperties() method and the method list all the properties (to n level) - name, value and type.
Currently, I have two methods (definations below):
public List<MyPropertyInfo> GetMyProperties(Type type);
public List<MyPropertyInfo> GetMyProperties(object obj);
I use the first one to show the list of all the parameters of the selected method along with it's properties - name, value and type.
MethodInfo members = type.GetMethod(this.MethodName);
ParameterInfo[] parameters = members.GetParameters();
List<MyPropertyInfo> oMyProp = new List<MyPropertyInfo>();
foreach (var parameter in parameters)
{
oMyProp = GetMyProperties(parameter.ParameterType);
}
..creating the list of my properties so that user can input the params. I pass ParameterType and GetProperties method checks if it is custom type or not. If custom type then it calls itself with the type recursively to build a list that I bind to a grid for input.
The second method GetMyProperties(object obj) is used to list the return object. Since I don't know the return type of the selected method at compile time so using object type. I want to know if I can modify the second method somehow to use it for reading the parameters, properties and return types? Instead of having separate methods? Trying to reuse the code.

I'm not sure I understand you correctly, but if you want to combine two methods in one, you can pass object in both cases, but check if object is Type or not and provide different logic for that cases:
public static void GetMyProperties(object obj)
{
if (obj is Type)
{
Type type = obj as Type;
... here is the first method logic ...
}
else
{
... here is the second method logic ...
}
}

Related

OutputCache varying by a complex object property passed using ModelBinder from Session

We are using Couchbase for our Session and for OutputCache.
In this context, how can we cache by a complex object that is being passed to the method using a Custom Model Binder that retrieves a value from the Session?
This is the signature of the method I want to cache with the OutputCache attribute:
[HttpGet]
[OutputCache(CacheProfile = "MyObjectsCache", VaryByParam = "myParam")]
public ActionResult Index([ModelBinder(typeof (CustomVariableSessionModelBinder<MyClass>))] MyClass myParam)
{
Note: The ModelBinder is being used here for reasons beyond me and I cannot change it.
MyClass is a complex object that has an Id. I want to use the Id as the caching identifier.
public class MyClass
{
public int Id{get;set;}
//Other Properties
This is how the object is being retrieved from Session:
var sessionKey = typeof (MyNamespace.MyClass).FullName;
var httpContext = HttpContext.Current;
MyNamespace.MyClass newObject = null;
if (httpContext.Session != null)
{
newObject = httpContext.Session[sessionKey] as MyNamespace.MyClass;
}
Is it possible yo use VaryByParam for this scenario or will I have to use VaryByCustom?
I haven't tested this, but it should work. It's pretty much your only option anyways, though.
In addition to the built in ways to vary, you can vary by "Custom". This will call into a method in Global.asax you'll need to override: GetVaryByCustomString. Importantly for you situation here, this method is passed HttpContext, so you should be able to look into the session. Essentially, the solution will look something like:
public override string GetVaryByCustomString(HttpContext context, string custom)
{
var args = custom.ToLower().Split(';');
var sb = new StringBuilder();
foreach (var arg in args)
{
switch (arg)
{
case "session":
var obj = // get your object from session
// now create some unique string to append
sb.AppendFormat("Session{0}", obj.Id);
}
}
return sb.ToString();
}
This is designed to handle multiple different types of "custom" vary types. For example, if you wanted to vary by "User", which is common, you can merely add a case for that in your switch. The important part is that the string returned by this method is actually what the output cache varies on, so you want that to be unique for the situation. This is why I prefixed the object's id with "Session" here. For example, if you just added the id, let's say 123, and then in another scenario you varied by user and that string was composed of just the user's id, which happened to be 123 as well. It would be the same string to the output cache, and you'd end with some weird results. Just be mindful of what the custom string looks like.
Now, you'd just alter your OutputCache attribute like:
[OutputCache(CacheProfile = "MyObjectsCache", VaryByParam = "myParam", VaryByCustom = "Session")]
Note: to vary by multiple custom things at once, you'd separate them with a ; (based on how the code above works). For example: VaryByCustom = "Session;User"

Create FileHelperEngine from Type variable

I am attempting to create an instance of FileHelperEngine<> using a generic type. For example this works for List
public static IList CreateList(Type type)
{
var genericListType = typeof(List<>).MakeGenericType(type);
return (IList)Activator.CreateInstance(genericListType);
}
Is it possible to do something similar for FileHelperEngine<>?
I tried
public static FileHelperEngine CreateFileHelperEngine(Type type)
{
var genericFileHelperEngineType = typeof (FileHelperEngine<>).MakeGenericType(type);
return (FileHelperEngine)Activator.CreateInstance(genericFileHelperEngineType);
}
And get this error
Unable to cast object of type 'FileHelpers.FileHelperEngine`1[NachaParser.Model.EntryDetail.PpdEntryModel]' to type 'FileHelpers.FileHelperEngine'.
This would not work because you are attempting to go from the generic engine to the standard engine. The generic engine does not inherit from the standard engine so you can't directly cast.
The following code should work for you:
public static FileHelperEngine<T> CreateFileHelperEngine<T>() where T : class
{
var genericFileHelperEngineType = typeof(FileHelperEngine<>).MakeGenericType(typeof(T));
return (FileHelperEngine<T>)Activator.CreateInstance(genericFileHelperEngineType);
}
The problem is that you need to have the type of T not as a Type variable but as as a passed generic argument. Unfortunately, the generic engine is based of EngineBase<T> with IFileHelperEngine<T> as an interface it implements so you can never get a FileHelperEngine<T> to a FileHelperEngine.
Your only other option is to use:
public static FileHelperEngine CreateFileHelperEngine(Type type)
{
if (!type.IsClass)
throw new InvalidCastException("Cannot use '" + type.FullName + "' as it is not a class");
return new FileHelperEngine(type);
}

VB.Net Merge property class from more than one web references

I have project that need to reference to some web service, just say my reference is
service1Facade and service2Facade
both of them contain class name objectA
i must load objectA from service1Facade and use it as parameter in service2Facade.
but i got error
"value of type service1Facade.objectA cannot be converted to service2Facade.objectA"
how can i convert these object ?
what i have try but still not work:
group all reference into same folder, but .NET change its name into
objectA and objectA1
I copy every property of the property inside objectA, but still not working.
The functionality that is responsible for generating proxy classes based on your WSDL specification doesn't know (and it shouldn't know) that both your services use the same underlying type for objectA, and as I mentioned, no assumptions can be made regarding this since web services are meant to be decoupled from each other (from the consumer point of view).
I'd say your best option is to have your own proxy class (let's say ServiceProxyDTO) that can be used in both service #1 and #2. Something along the lines of:
public class ServiceProxyDTO
{
// Define properties from "objectA"
public ServiceProxyDTO() { }
public ServiceProxyDTO(service1Facade.ObjectA copyFrom)
{
// Copy state from "copyFrom"
}
public ServiceProxyDTO(service2Facade.ObjectA copyFrom)
{
// Copy state from "copyFrom"
}
public static implicit operator service1Facade.ObjectA(ServiceProxyDTO dto)
{
return new service1Facade.ObjectA() { /* Copy state back */ };
}
public static implicit operator service2Facade.ObjectA(ServiceProxyDTO dto)
{
return new service2Facade.ObjectA() { /* Copy state back */ };
}
public static implicit operator ServiceProxyDTO(service1Facade.ObjectA obj)
{
return new ServiceProxyDTO(obj);
}
public static implicit operator ServiceProxyDTO(service2Facade.ObjectA obj)
{
return new ServiceProxyDTO(obj);
}
}
With this code you can instantiate ServiceProxyDTO and pass it as parameter to both service #1 and #2 (as well as get the return values from both of these services).
Hope this helps.

PexChoose non generic methods

Is there any way to specify the return type for PexChoose at runtime? For example PexChoose.Value(name, Type)?
This would be useful to make general models that generate values of different types depending on runtime contraints.
You could build your own helper class which will call the generic version via reflection.
For instance, to create a non-generic version of PexChoose.Value(string name)
public static class MyPexChoose
{
public static object Value(Type myType, string name)
{
// Find the PexChoose.Value() method which has a single string parameter
MethodInfo method = typeof(PexChoose).GetMethod("Value", new Type[1] {typeof(string)});
// Make and invoke the generic version of it
MethodInfo generic = method.MakeGenericMethod(myType);
return generic.Invoke(typeof(PexChoose), new object[1] { name });
}
}
Then the call
MyPexChoose(typeof(DateTime), "MyChosen");
is equivalent to
PexChoose<DateTime>("MyChosen");

How do I convert an object from Reflection to a generic collection?

I'm trying to write a Compare method to compare properties in some POCOs using Reflection to ensure that they've been persisted to the database correctly. For example, let's say I have this POCO:
public class NoahsArk
{
public string Owner { get; set; }
public ICollection<Animal> Animals { get; set; }
}
What I want to do is this:
[Test]
public class Saves_Correctly_To_Database()
{
var noahsArk = new NoahsArk { // some setup code here };
db.Save(noahsArk);
var dbNoahsArk = db.Get<NoahsArk>(noahsArk.Id);
Assert.That(Compare(noahsArk, dbNoahsArk), Is.True);
}
The ORM I'm using is NHibernate. My Compare method looks like this so far:
public static bool EqualsProperties<T>(this T x, T y)
{
var xType = x.GetType();
foreach (var property in xType.GetProperties())
{
if (property.GetValue(x, null).Implements(typeof(ICollection<>)))
{
var xValue = property.GetValue(x, null) as ICollection<T>;
var yValue = property.GetValue(y, null) as ICollection<T>;
}
Object.Implements() is an extension method I wrote to determine if a type implements an interface. As you can see, the method is incomplete. The problem I'm running into is that when I use property.GetValue(x, null), it returns an object, and I don't know how to cast it into its specific generic ICollection type. I need to be able to do this so I can use LINQ to do a x.Contains(y) to compare the two collections for equality. Any idea on how to do this?
P.S. I tried using Compare .NET Objects, but it's giving me a null reference exception somewhere deep within NHibernate. It doesn't properly handle how NHibernate proxies the ICollection for lazy loading. To make matters worse, NHibernate modifies the POCO to support lazy-loading, but this is all done at runtime. In the source code, it looks like you're just working with a regular ICollection, but NHibernate changes this to NHibernate.Collections.Generic.PersistentSet at runtime, and this is what's causing the comparer to fail.
Your question is a bit confusing because you don't need the type parameter T in the declaration of your EqualsProperties method. You just need
public static bool EqualsProperties(this object x, object y)
You then go on to use the same parameter T to cast properties of x and y to ICollection<T>; however, the objects in these collections obviously may have a different type than x and y.
Now to answer your question: you don't need to cast to the correct generic type to use the LINQ Contains method. You can do something like this:
xValue = property.GetValue(x, null);
yValue = property.GetValue(y, null);
if (typeof(IEnumerable).IsInstanceOf(x))
{
IEnumerable<object> xEnumerable = (x as IEnumerable).Cast<object>();
IEnumerable<object> yEnumerable = (y as IEnumerable).Cast<object>();
// use any LINQ method you like now
}
You should also make sure you use the LINQ overloads that take an equality comparer, as your domain objects obviously do not override the Equals method themselves. Otherwise you wouldn't be writing this unit testing code to compare them.
Sharp architecture framework use attribute to decor properties which should be taken into the equals method. See the source code of DomainSignatureAttribute class and EntityWithTypedId<>.Equals method.