Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Generics question/issues

$
0
0

Hi there. I am working on an abstract datamanager class that will take any class we throw at it and do simple crud interface with it.

I am having an issue with some generics in the save call and was wondering if anyone has come across this before.

I have an ID field in the generic class i want to access. (this would ideally fix most if not all the problems.)

I also am trying to access GetProperty on refections.ectensions but finding that requires ReflectionPermission which I am unsure where to code that/put that and how to utilize it.

if anyone has any thoughts that could point me in a direction that would be great.

Generic Class:
`public class GenericClass where T : class, new()
{
public int ID { get; set; }

    public GenericClass() 
    {
    }

} `

SaveItem Method:
`public async Task SaveItem() where T :class, new()
{
using (await Mutex.LockAsync().ConfigureAwait(false))
{
Type GenClass = typeof(GenericClass);

            //GenericClass<T> genClass = new GenericClass<T>();
            var existingItem = await _connection.ExecuteScalarAsync<T>("select top 1 from " + typeof(T).Name + "where ID = "); // + genClass.ID, null);
            if (existingItem == null)
            {
                await _connection.InsertAsync((Type)Activator.CreateInstance(typeof(T)).GetProperty("ID").c;// it breaks here.GetValue(null, null)).ConfigureAwait(false);
            }
            else {
                //T.ID = existingItem.ID;
                //await _connection.UpdateAsync(T).ConfigureAwait(false);
            }
        }
    } `

I am aware these are not ideal or optimal code at this time, for what I need to do it just needs to currently work. Again any help/thoughts would be great!!

edit (looks like markdown is broken for code.... so sorry for quality for code!)


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>