如何获得模块的'System.Type'?
例如模块:
module Foo =
let bar = 1
Run Code Online (Sandbox Code Playgroud)
这不起作用:
printfn "%s" typeof<Foo>.Name
Run Code Online (Sandbox Code Playgroud)
错误是:
The type 'Foo' is not defined
Run Code Online (Sandbox Code Playgroud) 我搜索过高低,但我无法想出解决方案.
我需要使用如下代码从程序集中获取所有接口类型:
IEnumerable<Type> interfaces = _assembly.GetTypes().Where(x => x.IsInterface);
Run Code Online (Sandbox Code Playgroud)
问题是,对于某些程序集,我遇到以下错误:
无法加载一个或多个请求的类型.检索LoaderExceptions属性以获取更多信息.
我完全清楚为什么会发生这种情况(未加载依赖程序集),以及如果我想对特定程序集进行故障排除,它是如何解决的.在我的情况下,我不知道前面的组装(用户将选择它).
我想知道的是,是否有任何方法可以让代码继续通过任何无法检索的类型,并且仍然可以提取那些不会失败的代码.
我有一个简单的类:
public class FilterParams
{
public string MeetingId { get; set; }
public int? ClientId { get; set; }
public string CustNum { get; set; }
public int AttendedAsFavor { get; set; }
public int Rating { get; set; }
public string Comments { get; set; }
public int Delete { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如何检查类中的每个属性,如果它们不是null(int)或empty/null(对于字符串),那么我将转换并将该属性的值添加到List<string>?
谢谢.
我想将Object转换为将在运行时分配的类型.
例如,假设我有一个函数将字符串值(从a TextBox or Dropdownlist)赋值给Object.Property.
如何将值转换为正确的类型?例如,它可以是整数,字符串或枚举.
Public void Foo(object obj,string propertyName,object value)
{
//Getting type of the property og object.
Type t= obj.GetType().GetProperty(propName).PropertyType;
//Now Setting the property to the value .
//But it raise an error,because sometimes type is int and value is "2"
//or type is enum (e.a: Gender.Male) and value is "Male"
//Suppose that always the cast is valid("2" can be converted to int 2)
obj.GetType().GetProperty(propName).SetValue(obj, value, null);
}
Run Code Online (Sandbox Code Playgroud) 该System.Type类型包含属性IsGenericTypeDefinition和ContainsGenericParameters.阅读完MSDN文档后,我得出结论,两个属性都存在,以检查类型是开放类型还是封闭类型.
但是,我没有看到两者之间的区别,以及何时想要使用另一个.
您好我正在尝试使用C#反射来调用传递参数的方法,并作为回报传递回结果.我怎样才能做到这一点?我尝试了几件事但没有成功.我已经习惯了PHP和Python,这可以在一行上完成,所以这对我来说非常困惑.
实质上,这就是如何在没有反射的情况下进行调用:
response = service.CreateAmbience(request);
Run Code Online (Sandbox Code Playgroud)
请求有以下对象:
request.UserId = (long)Constants.defaultAmbience["UserId"];
request.Ambience.CountryId = (long[])Constants.defaultAmbience["CountryId"];
request.Ambience.Name.DefaultText = (string)Constants.defaultAmbience["NameDefaultText"];
request.Ambience.Name.LanguageText = GetCultureTextLanguageText((string)Constants.defaultAmbience["NameCulture"], (string)Constants.defaultAmbience["NameText"]);
request.Ambience.Description.DefaultText = (string)Constants.defaultAmbience["DescriptionText"];
request.Ambience.Description.LanguageText = GetCultureTextLanguageText((string)Constants.defaultAmbience["DescriptionCulture"], (string)Constants.defaultAmbience["DescriptionDefaultText"]);
Run Code Online (Sandbox Code Playgroud)
这是我实现反射的函数,其中上面案例的serviceAction将是"CreateAmbience":
public static R ResponseHelper<T,R>(T request, String serviceAction)
{
ICMSCoreContentService service = new ContentServiceRef.CMSCoreContentServiceClient();
R response = default(R);
response = ???
}
Run Code Online (Sandbox Code Playgroud) 有没有人知道如何使用只有一个可选参数的构造函数来实例化一个类?
我试过了两个
(T)Activator.CreateInstance(typeof(T), new object[] { Type.Missing });
Run Code Online (Sandbox Code Playgroud)
和
(T)Activator.CreateInstance(typeof(T), new object[] { });
Run Code Online (Sandbox Code Playgroud)
没有成功......我遇到了MissingMethodException
如何在c#中找到调用方法的全名.我见过解决方案:
但他们只给我顶级水平.考虑这个例子:
namespace Sandbox
{
class Program
{
static void Main(string[] args)
{
test();
}
static void test()
{
var stackTrace = new StackTrace();
var methodBase = stackTrace.GetFrame(1).GetMethod();
Console.WriteLine(methodBase.Name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这只是输出'Main'如何让它打印'Sandbox.Program.Main'?
在任何人开始询问我为什么需要使用它之前,它是一个我正在处理的简单日志框架.
编辑
添加到Matzi的答案:
这是解决方案:
namespace Sandbox
{
class Program
{
static void Main(string[] args)
{
test();
}
static void test()
{
var stackTrace = new StackTrace();
var methodBase = stackTrace.GetFrame(1).GetMethod();
var Class = methodBase.ReflectedType;
var Namespace = Class.Namespace; //Added finding the namespace
Console.WriteLine(Namespace + …Run Code Online (Sandbox Code Playgroud) 对于.net 3.5我将v3.5传递给CSharpCodeProvider,当我在v4.5应用程序中将v4.5传递给CSharpCodeProvider时,我得到InvalidOperationException"无法找到编译器可执行文件csc.exe".
任何人都知道这里发生了什么,我做错了什么?
代码重现...
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
namespace Console1
{
class Program
{
static void Main(string[] args)
{
var options = new Dictionary<string, string>{{"CompilerVersion", "v4.5"}};
var cs = new CSharpCodeProvider(options);
var compilerParams = new CompilerParameters();
var r = cs.CompileAssemblyFromSource(compilerParams , "namespace ns { class program { public static Main(string[] args) { System.Console.WriteLine(\"Hello world\"); } } }");
}
}
}
Run Code Online (Sandbox Code Playgroud) 在Swift中,它不可能使用 .setValue(..., forKey: ...)
Int?enum类型的属性[MyObject?] 这有一个解决方法,即通过覆盖对象本身中的setValue forUndefinedKey方法.
因为我正在编写一个基于反射的通用对象映射器.请参阅EVReflection我希望尽可能地减少这种手动映射.
还有其他方法可以自动设置这些属性吗?
解决方法:可以在我的库中的单元测试发现这里 这是代码:
class WorkaroundsTests: XCTestCase {
func testWorkarounds() {
let json:String = "{\"nullableType\": 1,\"status\": 0, \"list\": [ {\"nullableType\": 2}, {\"nullableType\": 3}] }"
let status = Testobject(json: json)
XCTAssertTrue(status.nullableType == 1, "the nullableType should be 1")
XCTAssertTrue(status.status == .NotOK, "the status should be NotOK")
XCTAssertTrue(status.list.count == 2, "the list should have 2 items")
if status.list.count == 2 {
XCTAssertTrue(status.list[0]?.nullableType == 2, "the …Run Code Online (Sandbox Code Playgroud)