Portable Class Library Profile 78 missing attribute related methods/properties - c#

In my PCL core project (WP8, Android, iOS, Xamarin, MvvmCross) I use custom attributes. The Type.GetCustomAttributes() extension method lets me examine the attributes used.
Using PCL Profile104 this works well. But because I want to use async/await, I'll need to use PCL Profile78 (and .NET 4.5)
Problem: Seems the GetCustomAttributes() and the Attributes property are not available in Profile78. Why??
Note:
I am looking into the workaround by creating a PCL Profile 104 class library and wrapping the GetCustomAttributes() and then referencing this library from my PCL Profile78 library. However it seems extensionmethods are not supported...
Example Code:
public Pcl78Class()
{
Type t = this.GetType();
var attributes = t.Attributes;
var customAttributes = t.GetCustomAttributes(true);
// another weird thing: Why is VS CodeCompletion telling me it knows CustomAttributeExtensions class and methods?
//System.Reflection.CustomAttributeExtensions.GetCustomAttributes(t);
}

Problem: Seems the GetCustomAttributes() and the Attributes property are not available in Profile78. Why??
Profile78 includes support for Windows Store 8 (as noted on my blog), and Windows Store has a more efficient implementation of Type-based reflection. Essentially, you just have to call Type.GetTypeInfo to get a TypeInfo, and from there it should be pretty straightforward.

Related

How to build Xamarin Binding Library for estimote-sdk.aar

I would like to use the Estimote Beacon SDK in an Xamarin for Android Application. Since Xamarin's Estimote SDK [1] is very much outdated, I was thinking of including the official Estimote SDK [2] into the project which provides an estimote-sdk.aar file which I was trying to include and use. The Xamarin documentation suggests creating a Binding Library for that purpose [3].
So I was following those steps, made a Binding Library, added the .aar and set it's Build Action to LibraryProjectZip but I get several errors:
While I can solve the errors like
member names cannot be the same as their enclosing type
by renaming the members via Metadata.xml like so:
<attr path="/api/package[#name='com.estimote.sdk.cloud.model.google']/class[#name='Beacons']/field[#name='beacons']" name="name">BeaconsList</attr>
I don't know how to solve erros like
"Buffer" does not implement interface member "IBufferedSink.Buffer()"
How can this be solved?
What is the best approach to include the Estimote SDK into a Xamarin for Android application?
[1] Estimote SDK for Android by Xamarin Inc. https://components.xamarin.com/view/estimotesdkandroid
[2] Official Estimote SDK for Android https://github.com/Estimote/Android-SDK
[3] Binding an .AAR in Xamarin Developer portal https://developer.xamarin.com/guides/android/advanced_topics/binding-a-java-library/binding-an-aar/
Your best bet to support the Estimote SDK is to do the binding yourself. We do have a components team who tries to stay on top of every release, but as there are so many new releases and new libraries coming out everyday. It can serve quite a challenge.
"Buffer" does not implement interface member "IBufferedSink.Buffer()"
This typically means that the class is not implementing the respective interface member. You have a couple ways to go around this error.
Create a Partial class of your offending class and implement the
interface member.
Look at the api.xml to ensure the class is implementing all
members of the interface. Have a check at all the return types / arguments / etc to ensure they match the proper types. You would then have to change the managedReturn, managedType, or propertyName via Metadata.xml.
As a general reference, I have a small binding guide that you can find here:
https://gist.github.com/JonDouglas/dda6d8ace7d071b0e8cb
Specifically you might be most interested in common metadata fixes: https://gist.github.com/JonDouglas/dda6d8ace7d071b0e8cb#common-metadata-fixes

How to read class attribute on Windows Phone?

How can I read attribute of a class in Windows Phone?
public static void DoSth<T>(T sth)
where T : SomeClass
{
MyAttribute a = // Get "MyAttribute" from sth
}
Reflection works somewhat differently in .NET for Windows Store apps. You probably have to access this through System.Reflection.TypeInfo like so:
MyAttribute a = typeof(T).GetTypeInfo().GetCustomAttribute<MyAttribute>()
This article explains some of the differences in the Reflection APIs and why they exist.
In short, the authors wanted to expand the scenarios for Reflection and the design they settled on has a sort of "light-weight" Type class and a "heavy-weight" TypeInfo class. However for backwards compatibility in the full framework Type can still be used in the old heavy-weight way. On .NET for Windows Store, they had the chance make a single consistent API so they did that. Therefore Reflection code that works on .NET for Windows Store works in the full .NET framework but not the other way around.

Binding a native library in MonoTouch: works, but why strange results when subclassing? Missing NullAllowed attribute?

MonoTouch 5.2.13
I'm using a native ObjectiveC library via MonoTouch bindings. The library exposes a view controller. All is working fine, until I subclass the view controller. Then, I get NULL reference exceptions or part of the UI is not shown.
I have read about the problems with the btouch tool and the "-e" option. But from what I can see, the option is not present:
Tool /Developer/MonoTouch/usr/bin/btouch execution started with
arguments: /d:DEBUG ApiDefinition.cs /s:StructsAndEnums.cs
/tmpdir:obj/Debug/ios/ /sourceonly:obj/Debug/ios//sources.list
The subclass does (in this example) not add any functionality, but using it is enough to break the binding:
// Works.
var contr = new NativeController();
// Fails with various errors. Throws a NULL reference exception about some UIPopoverController being NULL, for instance.
public class SubclassedController : NativeController
{
public SubclassedController() : base()
{
}
}
var contr = new SubclassedController();
I figured out that adding the [NullAllowed] to a property solved the NULL reference exception.
The native controller exposes a property (nonatomic, strong) UIPopoverController *popoverController. This was bound as UIPopoverController PopoveController {get; set;} - adding the NullAllowed here solved the issue.
But now I'm facing issues with images not showing up and there is no obvious exception.
I assume these problems also derive from the fact that something is NULL when using the subclassed version but the native code seems to react on the NULL and handles it gracefully.
This leads to three questions:
Why is the behavior different when using subclassing? What is MonoTouch doing to make this happen? Why do I get a NULL ref in the case I use a derived class? I'm not touching the UIPopoverController an either case.
How can I find out what is wrong and how to fix it if there is no error?
What else has to be taken care of, besides the NullAllowed?
Why is the behavior different when using subclassing?
There can be several reasons, including the instance initializing itself differently based on it's class name. It's not hard to do in C# (very uncommon, inheritance is what's used) or Objective-C (delegates are often used to customize the behavior).
You'll need to refer to the documentation, header files, samples and product support of your native library to understand how subclassing works.
What is MonoTouch doing to make this happen?
I do not think it's MonoTouch related. MonoTouch provides btouch to help you create bindings for Objective-C libraries. This is the same tool that Xamarin uses to make all iOS bindings.
Still unsure ? write a small Objective-C sample that does the same. Compare results with the MonoTouch version.
Different results ? Fill a bug report with both (C# and ObjC) test case and we'll check what's going on (as stated before we have good reasons to ensure that btouch is bug free ;-)
Why do I get a NULL ref in the case I use a derived class? I'm not touching the UIPopoverController an either case.
How can I find out what is wrong and how to fix it if there is no error?
What else has to be taken care of, besides the NullAllowed?
At this stage you're running the native code of the library you binded. The specific rules you'll need to follow are the ones you'll find by:
reading the library documentation;
reading the library source (when available), headers and samples;
for [NullAllowed] you can test (e.g. unit tests) if null is allowed or not (e.g. will it crash);
Also porting existing samples is both a good learning exercise (of the library API) and a quick way to test for bindings bugs (e.g. typos in selectors, wrong parameters...).
This specific issue turned out to be two things:
A missing NullAllowed attribute for a UIPopoverController
A bug in MonoTouch 5.2.13 that is not related to binding but something else that has to do with CocoaTouch. The specific reason is unknown.
Everything is working as expected with MonoTouch 5.3.6

How to check equality of collection type properties, using reflection in C#

I have a class with many properties, some of them are lists of custom types.
I need to compare two instances of this class, and get a list of properties that aren't equal in them.
I designed it with custom attributes (for the relevant properties that I want to include in the comparison), and using the IEquatable, but for the lists I ran into problems. I was going to use SequenceEqual but it requires the type of the list (IEnumerable<SomeType>), which I don't have and don't know how to set. I'm aware of the GetElementType and GetGenericArguments methods but I can't use them inside IEnumerable<> to make the SequenceEqual work.
I'm looking for the best design for this scenario, and also code examples of how to actually do it.
This Codeplex project performs a deep compare of any two .NET objects using reflection:
http://comparenetobjects.codeplex.com/
Project Description
Perform a deep compare of any two .NET objects using reflection. Shows the differences between the two objects.
Compatiblity
Compatible with .NET Framework 3.5 and higher. New in 2.0, portable Class Library version works with .NET 4.0+, Silverlight 5+, Windows Phone 8+, Windows RT 8+, Xamarin iOS, and Xamarin Droid
NuGet Package
http://www.nuget.org/packages/CompareNETObjects

Reference Library for all C# .NET methods

There are a lot of built in methods and properties in C# and .NET, such as Trim() and Length(). Where can I find a complete list of these methods and properties for built-in types?
If you need a reference of the .NET class library and all the classes and methods it provides, MSDN is almost definitely the single most useful online resource — See e.g. .NET Framework Class Library Overview (introduction) and .NET Framework Class Library (reference library).
If all that you need is a reference of .NET string methods, again:
Go on MSDN
Find the reference page for the System.String class
Browse to the bottom and follow the link to the class members page: there you are.
.Net doesn't have "functions", everything you've listed is a method that is called on an object. Some are called on instances of objects, and some are called on the static type of the object.
Having a list of all functions would be pointless without knowing what objects they are related to.
There are also many overloads for methods, so they can be called with different parameters.
So what you really need (if you want them all) is a table of Object, Static or instance method, Method, Overloads.
You can also browse the methods from Object browser using the visual studio 2005 or latest versions.

Categories