I’m developing a mobile application which among other things, it receives data from medical devices via Bluetooth. To this end, I’m using an Android SDK in JAR via binding to my Xamarin project. Here’s the class that holds the returned data from the decompiled JAR:
package com.contec.jar.contec08a;
import java.util.ArrayList;
public class DeviceData
{
public ArrayList<byte[]> mData_blood = new ArrayList();
public ArrayList<byte[]> mData_oxygen = new ArrayList();
public ArrayList<byte[]> mData_normal_blood = new ArrayList();
}
What is of interest is the mData_blood. Each element of the array list corresponds to a patient. Each byte array is the medical data for each patient. The Xamarin framework makes some changes, i.e. the name of the property becomes MDataBlood.
Problem: when I receive the above class and property, the casting fails BUT NOT in the debugger. Here’s what I mean: the ‘as’ operator that implement the cast fails (returns null) but in the debugger the very same expression correctly shows the data.
What’s more, the ‘is’ operator returns false in runtime (the canDo variable is false) but true in the debugger. I’ve tried all methods of casting I’m familiar with, even using –but not mixing – Android objects. The highlighted line which casts the IList to List produces a nice exception. I’m at a total loss. Any suggestions would be greatly appreciated.
Here's the screenshot from the debugger which illustrates all the above:
Edit1: The MDataBlood[0] evaluates as generic Java object. When reviewing its properties, its isArray is set to true. By checking the decompiled source of the SDK, I determined that is indeed byte array.
In Xamarin, there are some castings that might fail when the objects come from java types. In that cases you should use JavaCast<TResult> instead of a regular casting.
Try this, instead of your current approach:
// Since MDataBlood is exposed as an IList property of MDeviceData,
// we first need to cast it to IJavaObject
var dataBloodRaw = (IJavaObject)dm.MDeviceData.MDataBlood;
// Then we have access to its JavaCast method
var dataBlood = dataBloodRaw.JavaCast<JavaList<byte[]>>();
JavaList<T> extends Java.Lang.Object, so it can be used as the TResult parameter for JavaCast<TResult>. Also, it implements IList, so you should be able to loop through it.
Anyway, if you need a List<byte[]> you can use the casted JavaList for creating it:
var dataBloodList = dataBlood.ToList();
Or, alternatively
var dataBloodList = new List<byte[]>(dataBlood);
Yet another thing you could just try is to Java-cast dataBloodRaw to a JavaList<object> and then loop through its objects, trying to cast each one of them as a byte[].
Plan B
If none of the above works, I suggest you to take a look at this answer where it is suggested to disable linking for Release, which can be done in your project properties:
Keep in mind that a side effect of this last option will be the impact on the final size of your application.
Related
I know templates are common within c/c++ however I am currently trying to translate an old VB code our company uses up to a c# equivalent which brings me to my problem....
VB is an type unsafe language so we come across things like
Public Elements As New Collection
so given the line above I need to translate that to a List.. Given that a Collection can be anything from a List to a map I need to template this as efficiently as possible. I was thinking of for first draft ignoring the map option entirely and just making it a List however from my understanding Templates don't truly exist within C#... The below code is what I came up with so far and while it compiles (I have not gotten to a testing point in the rest of the code yet) I don't know if it would actually work...
Public List<ValueType> Elements = new List<ValueType>();
can anyone offer input on if this would work as a generic list where type is determined at input or if I need to change this so the constructor would look more like
Public List<ValueType> Elements = new List<typeof(foo)>();
if the above is confusing I am sorry it is for me as well, and I will try and clarify as questions come in.
this question is no longer relevant, I was able to go into the source of the calling code and determine what variable types that the lists need to support.
I'm having an issue in a C# SQL CLR project where publicly declared Generic.List items are not showing up as public in the debugger. This is problematic because I'm trying to serialize the object to XML. The real project has some complex objects, but a very simple example would be something like this:
[Microsoft.SqlServer.Server.SqlProcedure]
public static void DoSomething()
{
// the Thing Object has a public property of List<string>
// that is created in the constructor
Thing t = new Thing();
t.Items.Add("test");
t.Items.Add("test2");
// now, if I look at t in the debugger, it has given the
// items list a capacity of 4 and shows the items as
// non-public members (2 with values and 2 that are null)
}
I wanted to post an image from the debugger here, but it appears I cannot do that as a new user...
With all of that said, is this behavior because it is a SQL CLR project? The same code worked fine when developing in a console application (though I guess I could have messed something up when adding it to the CLR project).
Adding additional detail on the Thing object - it includes the following declaration for the Items:
public List<string> Items { get; set; }
Then, in the debugger, the Items just shows Capacity and Count (no actual items or properties for the Items). The actual item instances are listed under Non-Public members.
The Items property itself can never be seen by a debugger, because it is an indexed property, so there isn't a value you can just get for it, and hence examining the value it returns doesn't make sense (like examining the 30th of February or a politician's kept promises; it just doesn't exist).
List<T> uses the DebuggerTypeProxyAttribute attribute to define another class to be used to give a debugger eye view. This class (one internal to mscorlib) has a public Items property that returns an array with a copy of the list's items, so that it looks like you can actually examine the Items property of List<T> when really you are examining the Items property of an object that copies out the items when invoked.
Debuggers don't have to use this approach, so maybe the one you are using doesn't, or there's some other restrictions, but if you are using a debugger that doesn't support DebuggerTypeProxy you can just examine the private member that stores the items in an array (the array will be at least as large as Count, and possibly a bit larger to leave growing room rather than resizing on each Add; you can ignore elements beyond Count - 1).
This has nothing to do with XML serialisation, so whatever problem you are having with that is likely completely unrelated.
Edit:
I see some people are having other problems with XML serialisation and the SQL CLR as per these:
SQL Server not finding serialization assembly
http://connect.microsoft.com/VisualStudio/feedback/details/753005/when-deploying-a-sqlclr-assembly-with-a-generated-xmlserializer-assembly-the-xmlserializer-assembly-should-be-deployed
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/3fa5dce3-b0f3-44f8-9b7b-65439f1c98ae/cannot-deploy-xmlserializers-clr-assemblies?forum=ssdt
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e6560fa4-76f1-4da2-b795-7926d0743baa/sql-clr-problem-with-xmlserializer?forum=sqlnetfx
Your XML problem is indeed different to your debugger view issue. (I meanwhile am off to add debugger proxies to some of my collection types, I've always figured people would just skip through to the inner members and not care, but you've made me rethink that one).
I have a class that interacts with a tableView. I have multiple pages in my program that uses this class/control. Problem was I was using List<> in this class to assign it to the itemsource. Later on I noticed I had to use BindingLists instead of Lists in order to be able to edit in the table for the end user.
Since I'm using different type of objects now I'm getting a lot of errors saying that it cannot implicitly convert to BindingList<object>, e.g.
Error 14 Cannot implicitly convert type 'System.ComponentModel.BindingList<Entity.Employee>' to 'System.ComponentModel.BindingList<object>' ...\frmMainWindow.xaml.cs
Which is really weird because it worked quite fine when I was using List.
I cannot declare the BindingList as Employee since I'm using more types.
Is there a way to still use BindingLists<> and works well like Lists<>?
EDITION:
List<vCalendarPeriod> temp_list = RPTDAL.Get_CalendarPeriodByCalendarName_View("Monthly");
BindingList<vCalendarPeriod> temp_blist = new BindingList<vCalendarPeriod>(temp_list);
catalogGridAdministrationCalendarsMonthly.ItemsSource = temp_blist;
The error is marking temp_blist, and the error is the one i described earlier.
I have X layers on a map and I need to intersect chosen layers (two per time) and color resulting features.
I'm trying to get this code working
// get first feature (index 0)
ESRI.ArcGIS.ADF.Web.Geometry.Geometry adfFeature =
m_firstLayer.GeometryFromRow(m_firstLayer.Rows[0])
as ESRI.ArcGIS.ADF.Web.Geometry.Geometry;
// THE FOLLOWING LINE RETURNS NULL
ESRI.ArcGIS.Geometry.IGeometry featureInterface =
adfFeature as ESRI.ArcGIS.Geometry.IGeometry;
ESRI.ArcGIS.Geometry.ITopologicalOperator topoOp =
adfFeature as ESRI.ArcGIS.Geometry.ITopologicalOperator;
How to use the IGeometry interface with an ADF Geometry object?
I can't really find samples to intersect features between two layers, and it's a pity that Spatial Joins are just a arcgis desktop function, I surely could use them!
I do not think you use the IGeometry directly. However if you know the specialized type (Point, Polyline, Polygon) you can convert (see the "Web ADF to ArcGIS Server ArcObjects" bullets) to ArcObjects.
You could test what subtype your ESRI.ArcGIS.ADF.Web.Geometry.Geometry adfFeature is and do conversion accordingly.
By the way I suggest that you never use the "as" cast since it can fail silently (just returning null). Instead I suggest:
ESRI.ArcGIS.Geometry.IGeometry featureInterface =
(ESRI.ArcGIS.Geometry.IGeometry)adfFeature;
Then you will see the problem as soon as it occurs.
So, I've been searching around on the internet for a bit, trying to see if someone has already invented the wheel here. What I want to do is write an integration test that will parse the current project, find all references to a certain method, find it's arguments, and then check the database for that argument. For example:
public interface IContentProvider
{
ContentItem GetContentFor(string descriptor);
}
public class ContentProvider : IContentProvider
{
public virtual ContentItem GetContentFor(string descriptor)
{
// Fetches Content from Database for descriptor and returns in
}
}
Any other class will get an IContentProvider injected into their constructor using IOC, such that they could write something like:
contentProvider.GetContentFor("SomeDescriptor");
contentProvider.GetContentFor("SomeOtherDescriptor");
Basically, the unit test finds all these references, find the set of text ["SomeDescriptor", "SomeOtherDescriptor"], and then I can check the database to make sure I have rows defined for those descriptors. Furthermore, the descriptors are hard coded.
I could make an enum value for all descriptors, but the enum would have thousands of possible options, and that seems like kinda a hack.
Now, this link on SO: How I can get all reference with Reflection + C# basically says it's impossible without some very advanced IL parsing. To clarify; I don't need Reflector or anything - it's just to be an automated test I can run so that if any other developers on my team check in code that calls for this content without creating the DB record, the test will fail.
Is this possible? If so, does anyone have a resource to look at or sample code to modify?
EDIT: Alternatively, perhaps a different method of doing this VS trying to find all references? The end result is I want a test to fail when the record doesnt exist.
This will be very difficult: your program may compute the value of the descriptor, which will mean your test is able to know which value are possible without executing said code.
I would suggest to change the way you program here, by using an enum type, or coding using the type safe enum pattern. This way, each and every use of a GetContentFor will be safe: the argument is part of the enum, and the languages type checker performs the check.
Your test can then easily iterate on the different enum fields, and check they are all declared in your database, very easily.
Adding a new content key requires editing the enum, but this is a small inconvenient you can live with, as it help a log ensuring all calls are safe.