system.reflection

Access data from main application within a plugin in C#

有些话、适合烂在心里 提交于 2019-12-06 14:31:11
问题 I am using this approach to make my C#-application extensible for other developers: Creating a simple plugin mechanism It works just fine but only in "one direction" which means that the developer who writes a new plugin can define methods and variables within the plugin and those will be imported to my application. So my question is now: How can i access already existing data from my main application (e.g. a variable "string test") within the plugin? 回答1: You need to create an "SDK" for your

How to use a dynamically generated object as the data source of CodeEffects generator

*爱你&永不变心* 提交于 2019-12-05 16:35:33
We are using this component www.codeeffects.com which allows us to create business rules based on the object properties. The html of the view is like this: @{ ViewBag.Title = "Post Example"; Html.CodeEffects().Styles() .SetTheme(ThemeType.Gray) .Render(); } @using (Html.BeginForm("Evaluate", "Post", FormMethod.Post)) { <div class="area"> <h1 class="title">Post Example</h1> <div style="margin-top:10px;"> <span>Info:</span> <span style="color:Red;">@ViewBag.Message</span> </div> <div style="margin-top:10px;"> @{ Html.CodeEffects().RuleEditor() .Id("ruleEditor") .SaveAction("Save", "Post")

Get List if Properties of type ICollection from Generic Class

大兔子大兔子 提交于 2019-12-05 12:20:34
I have an object that contains some ICollection type properties So basically the class looks like this: Class Employee { public ICollection<Address> Addresses {get;set;} public ICollection<Performance> Performances {get; set;} } The problem is get property names of type ICollection, inside of Generic class, by using reflection. My Generic Class is Class CRUD<TEntity> { public object Get() { var properties = typeof(TEntity).GetProperties().Where(m=m.GetType() == typeof(ICollection ) ... } But it is not working. How can I get a property here? GetProperties() returns a PropertyInfo[] . You then

Generate dynamic object from dictionary with C # Reflection

一曲冷凌霜 提交于 2019-12-05 07:08:34
I've been researching a bit about reflections in C # and would like to know if I use a dictionary with keys-values ​​can create an object with the variable with the name of each key in the dictionary and their values​​, the key value of that dictionary. I have a method that does the opposite, that extracts an object from a dictionary, this dictionary contains the keys and the class properties and their values​​, the value of the properties. I wonder how to do this if possible. Below is my method, which extracts a dictionary of an object: protected Dictionary<String, String> getObjectProperty

typeof(DateTime?).Name == Nullable`1

∥☆過路亽.° 提交于 2019-12-05 05:08:52
Using Reflection in .Net typeof(DateTime?).Name returns "Nullable`1". Is there any way to return the actual type as a string. (in this case "DateTime" or "System.DateTime") I understand that DateTime? is Nullable<DateTime> . That's besides the point, I am just looking for the type of the nullable type. There's a Nullable.GetUnderlyingType method which can help you in this case. Likely you'll end up wanting to make your own utility method because (I'm assuming) you'll be using both nullable and non-nullable types: public static string GetTypeName(Type type) { var nullableType = Nullable

How to Perform Reflection Operations using Roslyn

江枫思渺然 提交于 2019-12-05 03:30:46
问题 I would like to perform reflection style operations on the following class using Roslyn: public abstract class MyBaseClass { public bool Method1() { return true; } public bool Method2() { return true; } public void Method3() { } } Basically I want to do this, but with Roslyn: BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; MethodInfo[] mBaseClassMethods = typeof(MyBaseClass).GetMethods(flags); foreach (MethodInfo mi in mBaseClassMethods) { if (mi.GetParameters().Length == 0

transferring one object properties values to another one

时光毁灭记忆、已成空白 提交于 2019-12-05 02:04:05
问题 Before all, I know about AutoMapper , and I don't want to use it. Because I'm learning C# and I want to receive a deep view of it. So I'm trying to do this issue (explained below) myself. However, I'm trying to create a property copier to cope values of one type's properties to another one, if the property has the same name and type and is readable from source and writable in target. I'm using type.GetProperties() method. Sampled method is here: static void Transfer(object source, object

What does System.Reflection.TargetException: Non-static method requires a target. mean?

醉酒当歌 提交于 2019-12-05 01:45:28
问题 In my application I receive the functionCode value from somewhere and need to reflect the appropriate class. I tried to reflect the appropriate type According to this solution. but it doesn't work for me. I can not use GetField() method because I'm working on a PCL project. Therefore I tried these lines of code: AssemblyName name = new AssemblyName("MyLibrary"); var type = Assembly.Load(name); type.DefinedTypes.FirstOrDefault(x => x.GetDeclaredProperty("functionCode") != null && (byte)x

CreateType missing from TypeBuilder. How to port this?

青春壹個敷衍的年華 提交于 2019-12-05 00:53:48
Trying to port an application from .net 4.5 to .net core for a client. I'm noticing that CreateType is no longer part of TypeBuilder. I've searched through multiple of the new reflection libs with no luck. Does anyone know how to port this? Code in question: typeBuilder.CreateType() Zoey M I found the answer, but in a different repository than I expected. CreateType was dropped, and CreateTypeInfo should be used now per this: https://github.com/dotnet/coreclr/issues/2222 'TypeBuilder' does not contain a definition for 'CreateType' and no extension method 'CreateType' accepting a first argument

Access data from main application within a plugin in C#

故事扮演 提交于 2019-12-04 19:06:38
I am using this approach to make my C#-application extensible for other developers: Creating a simple plugin mechanism It works just fine but only in "one direction" which means that the developer who writes a new plugin can define methods and variables within the plugin and those will be imported to my application. So my question is now: How can i access already existing data from my main application (e.g. a variable "string test") within the plugin? You need to create an "SDK" for your application which is available to your plugin writers. This should contain interfaces for anything you want