C# paste T model class dynamically as variable [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to store my model class as a variable and use it in all my code dynamically. For example I have something like this:
IEnumerable<Student> students = GetAllStudents();
Normally I must always declare and retype Student class. But I need to declare it only once and after that use it dynamically in my code. Simply I want to do something like this:
Student dynamicType;
So after that can be possible:
IEnumerable<dynamicType> students = GetAllStudents();
Can you help me to find right solution? Is it possible to do something like this?
Thank you

It depends what you mean by "dynamic".
If you just don't want to use the class name "Students", then you're looking for a Type Alias.
using dynamicType = MyNameSpace.Student;
...
IEnumerable<dynamicType> students = GetAllStudents();
If you want to be able to specify which type you're dealing with from another part of code, you're looking for Generics.
void DoSomething<T>()
{
IEnumerable<T> things = GetAllThings<T>();
...
}
...
DoSomething<Student>();
If you want to be able to interact with the class without having any specific type associated with it, you're looking for the dynamic keyword.
IEnumerable<dynamic> students = GetAllStudents();
foreach(var student in students)
{
dynamic studentFoo = student.foo; // no type checking
}
If you want to avoid typing "Student" everywhere, but you really do want the items in your IEnumerable to be statically-checked Students, then you may just want to use the var keyword.\
var students = GetAllStudents();

Related

possible I am writing the wrong method for what i am doing in unity c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I honestly could not think of a title...anyways
I am trying to create a type
public enum AssemblyType{
Name = "NameOfFile.dll",
}
In then in the unity inspector I would have a list of this type
public List<AssemblyType> requiredAssemblies = new List<AssemblyType> ();
And my expectation is that I will see a list where I can set a list of dropdown boxes to the Name of the AssemblyType
For example this
Inside of this
In case you dont get the image
Day Colours would be the list requiredAssemblies and size would be like Culling Mask on the first image
Sorry forgot what went wrong with the above method
This method returns this error in unity
Assets/Scripts/WebSharp.cs(21,16): error CS0029: Cannot implicitly convert typestring' to int'
sorry my problem was clearly stated
from the error I would(as anyone i hope) realize that an enum is an int value
My question what should I be replacing the enum with since a string cannot exist outside of a class
This
public enum AssemblyType{
Name = "NameOfFile.dll",
}
...doesn't compile. So I'm not sure what the question is.
If you're just trying to define a list of DLL names, you just need to replace this
public List<AssemblyType> requiredAssemblies = new List<AssemblyType> ();
with this
public List<string> requiredAssemblies = new List<string>
{
"NameOfFile.dll",
"NameOfOtherFile.dll",
"Et cetera"
};
If you need to expose a simpler name to the user, but let the application remember the original name, you can use a dictionary:
public Dictionary<string,string> requiredAssemblies = new Dictionary<string,string>
{
{"NameOfFile1.dll",#"c:\SomeLongPath\NameOfFile2.dll"},
{"NameOfFile2.dll",#"c:\SomeLongPath\NameOfFile2.dll"},
{"NameOfFile3.dll",#"c:\SomeLongPath\NameOfFile3.dll"}
};
..and then populate your controls using the list returned by requiredAssemblies.Keys. You can then use the user's selection to look up the full path in the dictionary when you need it.

class.forName equivalent in c# without creating an instance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to dynamically access classes in c# in the way like Java class.forName(). I have only found things like Class.forName() equivalent in .NET? but I don't want to create instances.
In detail: I have got a simple text file containing a list of classes. I read them using file.ReadLine() (so I have got all class names as strings) and then I want to execute the same static method on each class: class1.method(); class2.method; and so on. The classes all exist and I need to access them. How can I do that?
C# doesn't support static interfaces (or static members in interfaces), so unless you want to use factory classes (the usual approach), you'll need to use reflection to invoke the static method directly.
void Main()
{
Console.WriteLine(Type.GetType("A").GetMethod("Hi").Invoke(null, new object[] {}));
}
class A
{
public static string Hi() { return "Hi!"; }
}
You might want to use a fully-qualified name for the type to make this work really well. Using just the name of the type is tricky, especially when you're trying to invoke types from other assemblies (which you probably are, otherwise there'd be no reason to use reflection - just use a dictionary of delegates or whatever).
You can use System.Reflection to load the Assembly into memory and then drill down to the type followed by getting the required method and invoke.
Refer to the documentation
GetMethod
If you have names of your desired Type then you can use Type.GetType(string) method.
Example if you have a class like this :
namespace MeProgram.BusinessLogic
{
public class MeObject {}
}
Full class name of that object should be "MeProgram.BusinessLogic.MeObject".
Now you can use that name inside of Type.GetType(string) method like such :
string className = "MeProgram.BusinessLogic.MeObject";
Type classType = Type.GetType(className);

How do you store C# Property in SQL Server database and invoke command? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I would assume it wouldn't be complicated to do the following:
I have a VARCHAR database field that will store a C# property such as "DateTime.Now.Year". I want to pass this value into my ASP.NET application and dynamically return 2017.
How do I read the VARCHAR value as a string and get the C# method to invoke the property?
You could achieve this using System.Reflection, but you really need to store information about the assembly, class and property/field/method you want to invoke.
for example if you stored:
AssemblyPath: "c:\something\someassembly.dll"
ClassFullPath: "SomeAssembly.SomeNameSpace.SomeClass, SomeClass"
MethodName: "someMethodName"
Then in your code you could attempt to load the assembly and instantiate the class:
var assembly = Assembly.Load(assemblyPath);
var clss = Activator.CreateInstance(ClassFullPath);
var method = clss.GetType().GetMethod(MethodName);
var result = method.Invoke();
Now that is a super simplified example, and there are many things to consider like access to the method / property / field etc. (i.e. private vs public .. and static vs instance)
So if you can really reduce your cases and design a set of flags / options you can store in your table, it will be possible.

Serialize to JSON Linq select result "WhereSelectArrayIterator" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to serialise only some fields from Offices, only Name and ID
public IEnumerable<Office> Offices;
public string SerializedPointOffices
{
get { return JsonConvert.SerializeObject(Offices ); }
}
I have one Linq select
IEnumerable<MyObj> offices = GetAllOffices();
var test = offices.Select(t => new { t.OfficeName, t.OfficeID});
How Serialize this test?
type of test is WhereSelectArrayIterator
I am not entirely sure of your question, however, assuming you want to serialize an array or list of anonymous types to JSON, its pretty simple.
as above:
var test = offices.Select(t => new { t.OfficeName, t.OfficeID});
string str = JsonConvert.SerializeObject(test);
You can also use the Data Contract serialize annotations on your office object so that only the fields you want get serialized when you serialize the object. Personally, I prefer that way because I don't like sending anonymous types over the wire, I like having a strongly defined interface.

groupBy on multiple columns using my innerClass [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I try to group by StringId and then by Name
var a = myList
.GroupBy(item => new TcGroupByKey()
{StringId = item.Id, ChannelName = item.ChannelName}).ToList();
I have created this inner class TcGroupByKey
so I can pass the result to TrackingChannelRow ctor
and will get strongly type argument and not object
public TrackingChannelRow(ManageTcModel.TcGroupByKey tcGroupByKey,
IGrouping<ManageTcModel.TcGroupByKey, TrackingChannelItem> subChannels,
IEnumerable<Manager.TrackingChannels.Model.ToolbarItem> toolbars,
IEnumerable<Manager.TrackingChannels.Model.BundleItem> bundles)
{
But the group by doesn't work.
What am I doing wrong?
I think that what you're looking for is to group by composite key...
Something similar to what Marc described in this post Linq to SQL with Group by
The key is to use 'anonymous' types - not the named one like you did - that LINQ can translate into SQL, otherwise there's an issue and that code can only run 'post SQL' (so you enumerate first then group but lose on the SQL integration, performance).
So, in your case it'd be something like this...
new {StringId = item.Id, ChannelName = item.ChannelName}
...instead of new TcGroupByKey() {StringId = item.Id, ChannelName = item.ChannelName}
There is similar problem with doing a 'Select' into anonymous (works) or named type (doesn't - unless, as I mentioned above, you separate the SQL and C#-only expressions part, a bit simplified).
hope this helps
You are asking linq to group your list by an object he doesn't recognize.
What's wrong with:
var a = myList.GroupBy(Id).GroupBy(ChannelName).ToList();

Categories