Note that there are some explanatory texts on larger screens.

plurals
  1. POHowto reflect object into an only at runtime known type
    primarykey
    data
    text
    <p>i have a little big problem.<br> My problem is the following. </p> <p>I´ve got two types: AssetData and AssetData. They are basicly the same, but not inherited.<br> Now i have i know a property with the Type "AssetData", and i have a object of type AssetData (containing a Texture2D object). </p> <p>Now i want to cast the AssetData object into the AssetData object. Because i don´t know the generic parameter, AssetData has now operator for that kind of cast, but AssetData is able to cast to AssetData. </p> <p>I tried and tried what i could do to solve it, but i have no more ideas.</p> <p>Here´s my situation: I´ve got a type of a property, i have a AssetData with the same object, but i must set the AssetData to the property - so i have to set "AssetData", not AssetData.</p> <p>Here´s my code. I can´t hardcode it, because it would be to much to change after adding new types. That´s my latest try. It nearly works, but has the problem that the cast does not work, because there´s no operator of AssetData ...</p> <pre><code>foreach (PropertyInfo pinf in comp.GetType().GetProperties()) { for (int i = 0; i &lt; cdata.PInf.Count; i++) { if (cdata.PInf[i] != pinf.Name) continue; AssetData assetData = new AssetData { AssetName = cdata.AN[i], AssetType = Type.GetType(cdata.AT[i], true) }; Application.Game.GetSystem&lt;ContentManager&gt;().LoadContent(ref assetData); if (pinf.PropertyType.IsGenericType) { MethodInfo method = typeof (DynamicCast).GetMethod("Cast").GetGenericMethodDefinition().MakeGenericMethod( assetData.AssetType); Type castedAssetType = pinf.PropertyType.GetGenericTypeDefinition().MakeGenericType(assetData.AssetType); dynamic castedAsset = method.Invoke(typeof (DynamicCast), new[] {assetData}); pinf.SetValue(comp, castedAsset, null); } } } } </code></pre> <p>And here´s the method of "DynamicCast" - which i found on an blog. This also doesn´t work ...</p> <pre><code> public static class DynamicCast { public static T Cast&lt;T&gt;(object o) { Type ot = o.GetType(); MethodInfo meth = GetMethod(ot, "op_Implicit", typeof(T), BindingFlags.Static | BindingFlags.Public); if (meth == null) { meth = GetMethod(ot, "op_Explicit", typeof(T), BindingFlags.Static | BindingFlags.Public); } if (meth == null) throw new InvalidCastException("Invalid Cast."); return (T) meth.Invoke(null, new[] {o}); } public static MethodInfo GetMethod(Type toSearch, string methodName, Type returnType, BindingFlags bindingFlags) { return Array.Find( toSearch.GetMethods(bindingFlags), inf =&gt; ((inf.Name == methodName) &amp;&amp; (inf.ReturnType == returnType))); } } </code></pre> <p>The problem is, that i must create an AssetData object (in this case), and set it as value for the property. But the object is clear, so i must cast AssetData´s "Asset"-property to the "AssetData"´s. Both are the same type, but one is "object" and one "T" (Texture2D).</p> <p>How can i cast this?</p> <p>Thanks a lot! I´m workin on this since the midday ...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload