Reputation: 482
I am currently trying to develop a mobile barcode reader in Windows Phone 7.5 using the ZXing library. Seeing that I am posting here, you might already have guessed that I am facing some kind of problem that I don't know any solution to.
The problem is the following: The ZXing library allows for multiple barcode formats - however, I'd like to include a settings menu for the user to focus on one barcode specifically.
The BarcodeFormat-object is static and contains the readonly members (of type BarcodeFormat) that I want to assign.
Seeing and hearing that Reflection is THE powerful weapon for dynamic behaviour like this, I thought I'd give it a try.
So far I have code that gets all the possible formats from ZXing using
MemberInfo[] plist = typeof(BarcodeFormat).GetMembers();
That works for getting the names of the formats, meaning I can successfully show the names in a list. But I am running into a problem when trying to assign these formats to the actual reader, because I only have the MemberInfo and no longer the BarcodeFormat.
So far I have only found examples where the user wanted to access (set / get) variables dynamically. The proposed solutions however did not seem to fit my problem - at least I didn't find any way to assign the format in those ways.
Any help would be great :) Thank you very much.
EDIT: The BarcodeFormat is used like this:
WP7BarcodeManager.ScanMode = BarcodeFormat.ITF;
In this example, only barcodes in the ITF (interleaved 2 out of 5) format would be accepted.
I have so far tried the following approaches.
Simply assign the MemberInfo object instead of the original BarcodeFormat object.
Cast the MemberInfo object to BarcodeFormat.
I tried to use FieldInfo and getValue, however it seems that I have to create an example object and assign a value to the needed field in order to get the value. This can't be done here, because the object is static and the field is readonly.
The whole ZXing library is compiled as a DLL that I link my project to. (it seems to be linked correctly, because everything else works). An example declaration of BarcodeFormat looks like this
public static readonly BarcodeFormat ITF
Upvotes: 0
Views: 3299
Reputation: 4680
For Example :
PropertyInfo CurCultProp = (typeof(CultureInfo)).GetProperty("CurrentCulture");
Console.WriteLine("CurrCult: " + CurCultProp.GetValue(null,null));
So all you need to do is call GetProperties()
instead of GetMembers()
and call GetValue(null, null)
to get the value.
Upvotes: 3
Reputation: 28325
I don't fully understand why you go through the hassle with reflection.
You can enumerate the bar code types like this (ok dummy code, you should probably bind to a listbox/picker but.. ):
var mgr = new BarcodeTypeManager();
foreach (var barCode in mgr.BarcodeTypes)
{
WP7BarcodeManager.ScanMode = barCode.BarcodeType;
}
(In fact, there's also a BarcodePhotoChooser
picker you can use.)
And if the user can save a preferred type, you can easily look it up again:
var typeToUse = mgr.BarcodeTypes.Where(b => b.Name == "what user selected").FirstOrDefault();
WP7BarcodeManager.ScanMode = typeToUse.BarcodeType;
Upvotes: 1
Reputation: 16981
get ITF dynamically:
var formatName = "ITF";
var format = typeof(BarcodeFormat)
.GetProperty(formatName, BindingFlags.Static | BindingFlags.Public)
.GetValue(null, null);
set WP7BarcodeManager.ScanMode:
WP7BarcodeManager.ScanMode = (BarcodeFormat)format;
ps
member to BarcodeFormat:
var _format = member is PropertyInfo
? ((PropertyInfo)member).GetValue(null, null)
: ((FieldInfo)member).GetValue(null);
var format = (BarcodeFormat)_format;
Upvotes: 3