I’m trying to use MapProvider.TryGetProvider(int guid) and it always return null for all of the maps.
When I create the map object and initial it with any provider the map load successfully without any problem but when I use TryGetProvider for the same provider it return null.
Thank you for your help
Map.MapProvider = MapProvider.TryGetProvider(value.GetType().GUID)
UPDATE:
I did success to reload the map provider by the next code line:
_selectedMapProvider = value; var assemblyType = Assembly.Load("GMap.NET.Core"); Map.MapProvider = (GMapProvider)assemblyType.GetType($"GMap.NET.MapProviders.{SelectedMapProvider.GetType().Name}").GetField("Instance").GetValue(null); Map.ReloadMap();
But I’m trying to use something more fast then use reflection, like the TryGetProvider
Related
I'm calling an API the same way I always have called APIs. The response is what I would expect except with every other API I've used it's returned an XML string which is easy to get data out of. This one particular API seems to return the response and place the response data into an object. Here is how I'm calling the API in C#...
KeyFieldsAPI.PS_API_KeyFields_V2 KeyFieldsAPI = new KeyFieldsAPI.PS_API_KeyFields_V2();
object KFAPI = KeyFieldsAPI.GetKeyFields(KAccount_Number, KCourier, KService, "", InventoryCodeList);
I can loop through the KFAPI object and place the upper level field responses into variables by using:
foreach (PropertyInfo property in KFAPI.GetType().GetProperties())
{
PropName = property.Name.ToString();
PropType = property.GetType().ToString();
PropValue = property.GetValue(KFAPI, null).ToString();
DataRow dr = ITAB_KFHEADER.NewRow();
dr["Name"] = PropName;
dr["Value"] = PropValue;
ITAB_KFHEADER.Rows.Add(dr);
}
However two of the fields seem to be arrays as seen here...
image of watch values box
I need to extract data from the NeedDate_Item field which seems to be an array with 3 index lines of data as part of this object. Can anyone suggest how I can extract data from this API response object that is part of an embedded array? Thanks to everyone in advance.
I figured it out. If I change the line of code to call the API from object to var like this:
var KFAPI = KeyFieldsAPI.GetKeyFields(KAccount_Number, KCourier, KService, "", InventoryCodeList);
Then it allows me to grab the data like this:
foreach (var NDdate in KFAPI.NeedDate_Item)
{ *** do work here***
}
Calling the API with "object" did not allow me to select and use the embedded arrays but changing the call to "var" did. Maybe this might help someone else in the future.
I have a problem, under http://www.pathofexile.com/api/public-stash-tabs link there is a huge API that returns a JSON string. Many of fields in this JSON are optional, that means they only appear if there is value present.
So theoretical "Item1" can have "abyssJewel" property but
"item2" doesnt have to have "abyssJewel" property
When i try to query this JSON with JSON.Linq like this
AbyssJewel = (bool)item["abyssJewel"];
in the case of Item1 everything is good and it returns proper value
but in case of Item2 i get exception "InvalidOperationException, Cannot access child value on Newtonsoft.Json.Linq.JProperty"
I understand its because for Item2 no abyssJewel property in JSON exists so it throws exception.
My question is this, how can i handle it so that instead of throwing exception it would return a default or null value for this particular field?
I have tried playing with Activator but couldnt make anything working on my own. Any tips?
im instantiating it like this:
apiPages.Add(new Page
{
Next_Change_Id = (string)jsonObject["next_change_id"],
Stashes = jsonObject["stashes"].Select(stash => new Stash
{
AccountName = (string)stash["accountName"],
StashName = (string)stash["stash"],
StashType = (string)stash["stashType"],
Public = (bool)stash["public"],
LastCharacterName = (string)stash["lastCharacterName"],
UniqueId = (string)stash["id"],
Items = stash.Select(item => new Item
{
AbyssJewel = (bool)item["abyssJewel"],
...tl;dr...
Instead of casting directly you should try to use the TryParse() method from the Boolean class, if something goes wrong it must return false. See here
Hope it will fix your problem.
When I am checking my cache to see if if my ValueTuple is in the cache, I use the code below. In this scenario, the value coming back is null (aka does not exist in the cache). When the code runs I get object not set to instance of an object error on the 1st line. Is there a proper way to cast an object to a ValueTuple?
var geo = ((double, double))CacheEngine.Get(key);
if (!geo.Equals(default(ValueTuple<double, double>))) return geo;
I found that I needed to do it like this to work. You need to cast to ValueTuple<double, double> and not (double, double).
var geo = CacheEngine.Get(key);
if (geo != null)
{
var geoTuple = (ValueTuple<double, double>)geo;
if (!geoTuple.Equals(default(ValueTuple<double, double>)))
return geoTuple;
}
I'm trying to extend/copy this code by
marisks
to include a default sorting interceptor.
But for the life of me, I cannot wrap my head around the expressions and bindings.
Inspired by the SoftDelete code
var table = (EntityType)expression.Target.ElementType;
if (table.Properties.All(p => p.Name != IsDeletedColumnName))
{
return base.Visit(expression);
}
var binding = expression.Bind();
return binding.Filter(
binding.VariableType
.Variable(binding.VariableName)
.Property(IsDeletedColumnName)
.IsNull()
);
I'm trying to add a similar thing to sort data be default.
This is the closest I've come (that will compile)
var table = (EntityType)expression.Target.ElementType;
string sortingColumn = "Priority";
var binding = expression.Bind();
return binding.Filter(
binding.VariableType
.Variable(binding.VariableName)
.Property(sortingColumn)
.OrderBy(m => m)
);
but it throws an exception at runtime:
DbExpressionBinding requires an input expression with a collection ResultType.
Parameter name: input
Can someone help me fix this - and possibly help me understand what's going on?
I did some hacking on my own. Basically the problem is that you try to create a Filter expression, but you need a Sort expression. Something like this:
var sortingColumn = "OrderProp";
var binding = expression.Bind();
return DbExpressionBuilder.Sort(binding,
new[] {
DbExpressionBuilder.ToSortClause(binding.VariableType.Variable(binding.VariableName).Property(sortingColumn) )
});
Of course, this only works, if you have the "OrderProp" on the type itself (you have to add extra checks for that).
I’m looking to take a dynamic object from a third party API & convert to my C# object with minimal code.
Sort of like what Dapper does with a SQL statement results to a C# object. Is there a library that would do this?
I can do it manually like below, but it seems like alot of code to write everytime I want to retrieve from this API.
public IEnumerable<Requirement> Get()
{
dynamic requirementsSelect;
requirementsSelect = _RequirementsRepository.GetRequirements();
IList<Requirement> Requirements = new List<Requirement>();
foreach (var req in requirementsSelect)
{
Requirement requirement = new Requirement();
requirement.Text = req.Text;
requirement.Number = req.Number;
Requirements.Add(requirement);
foreach (var node in req.Phrases)
{
Requirement reqNode = new Requirement();
reqNode.Text = node.Text;
reqNode.Number = node.Number;
requirement.Nodes.Add(reqNode);
}
return Requirements;
}
Thanks
I ended up returning the third party as JSON as follows. In my specific case the JSON wasn't showing the properties. I resolved by changing the dynamic object to IEnumerable and I was then able to specify which properties I wanted to retrieve using .Select.
IEnumerable<dynamic> requirements = _RequirementsRepository.GetRequirements();
return JsonConvert.SerializeObject(new
{
Requirement = requirements.Select(x => x.Text),
Number = requirements.Select(x => x.Number),
});
Thanks!